28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
function parse(iso: string): Date {
|
||
return new Date(`${iso}T00:00:00`)
|
||
}
|
||
|
||
/** "Apr 2025 – Oct 2026", or "Oct 2024 – today" when there is no end date */
|
||
export function cvPeriod(from: string, to: string | null, lang: CvLang, t: CvText): string {
|
||
const locale = lang === 'de' ? 'de-DE' : 'en-GB'
|
||
const month = (iso: string) => parse(iso).toLocaleDateString(locale, { month: 'short', year: 'numeric' })
|
||
return `${month(from)} – ${to ? month(to) : t.today}`
|
||
}
|
||
|
||
/** "1 year 6 months", counted until today when there is no end date */
|
||
export function cvDuration(from: string, to: string | null, t: CvText): string {
|
||
const start = parse(from)
|
||
// The end day counts, so 1 Apr – 31 Oct is 7 months, not 6
|
||
const end = to ? parse(to) : new Date()
|
||
end.setDate(end.getDate() + 1)
|
||
let total = (end.getFullYear() - start.getFullYear()) * 12 + end.getMonth() - start.getMonth()
|
||
if (end.getDate() < start.getDate()) total--
|
||
total = Math.max(1, total)
|
||
const years = Math.floor(total / 12)
|
||
const months = total % 12
|
||
return [
|
||
years && `${years} ${t.year[years === 1 ? 0 : 1]}`,
|
||
months && `${months} ${t.month[months === 1 ? 0 : 1]}`
|
||
].filter(Boolean).join(' ')
|
||
}
|