feat: formatMonthYearRange — period now includes abbreviated month

This commit is contained in:
Ilia Mashkov
2026-05-18 13:01:58 +03:00
parent 1550989fd9
commit 48a08ec3fb
4 changed files with 52 additions and 45 deletions
+17 -10
View File
@@ -1,31 +1,38 @@
const MONTH_FMT = new Intl.DateTimeFormat('en-US', { month: 'short', year: 'numeric', timeZone: 'UTC' });
function formatMonthYear(date: Date): string {
return MONTH_FMT.format(date);
}
/**
* Formats a PocketBase date string into a localized year string or "Present".
* Formats a PocketBase date string into a localized month+year range or "Present".
* @throws {Error} if any date is invalid or if the range is logically impossible.
*/
export function formatYearRange(start: string, end: string | null): string {
export function formatMonthYearRange(start: string, end: string | null): string {
const startDate = new Date(start);
if (Number.isNaN(startDate.getTime())) {
throw new Error('Invalid start date');
}
const startYear = startDate.getFullYear();
if (end === null) {
return `${startYear} — Present`;
return `${formatMonthYear(startDate)} — Present`;
}
const endDate = new Date(end);
if (Number.isNaN(endDate.getTime())) {
throw new Error('Invalid end date');
}
const endYear = endDate.getFullYear();
if (startYear > endYear) {
throw new Error('Start year cannot be after end year');
if (startDate > endDate) {
throw new Error('Start date cannot be after end date');
}
if (startYear === endYear) {
return `${startYear}`;
const startLabel = formatMonthYear(startDate);
const endLabel = formatMonthYear(endDate);
if (startLabel === endLabel) {
return startLabel;
}
return `${startYear}${endYear}`;
return `${startLabel}${endLabel}`;
}