km-web-plugin
Version:
ICE Web Plugin Initializer
36 lines (29 loc) • 1.04 kB
text/typescript
export function formatDateToMMDDYYYY(
date: Date | null | undefined,
): string | undefined {
if (!date) return undefined;
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const year = date.getFullYear().toString();
return `${month}/${day}/${year}`;
}
export function parseDateFromMMDDYYYY(
dateString: string | undefined,
): Date | undefined {
if (!dateString) return undefined;
// Try to parse MM/DD/YYYY format specifically
const mmddyyyyPattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
const match = dateString.match(mmddyyyyPattern);
if (match) {
const [, month, day, year] = match;
const date = new Date(
parseInt(year),
parseInt(month) - 1,
parseInt(day),
);
return isNaN(date.getTime()) ? undefined : date;
}
// Fallback to general Date parsing
const date = new Date(dateString);
return isNaN(date.getTime()) ? undefined : date;
}