export function GetNumber(value: any, defaultValue: number = 0): number { return (value == undefined || value == null || isNaN(value)) ? defaultValue : Number(value); } export function IsNull(value: unknown): boolean { // return value === null || typeof value === 'undefined'; return value === null || value === undefined; } export function IsNotNull(value: unknown): boolean { return !IsNull(value); } /** * Returns age as a human-readable string in years, months, weeks and days. * E.g. "10 years, 5 months, 1 week, 2 days". * If DOB is in the future or invalid, returns "0 days". * * @param DOB Date or parsable date-string of birth * @param referenceDate Date to calculate age against (default: now) */ export function GetAgeString( DOB: Date | string, referenceDate: Date = new Date() ): string { // 1) Parse & validate DOB const birthDate = DOB instanceof Date ? new Date(DOB.getTime()) : new Date(DOB); if (isNaN(birthDate.getTime())) { // invalid date return '0 days'; } // 2) Normalize both dates to UTC midnight (avoid DST/timezone issues) function toUtcMidnight(d: Date) { return new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); } const start = toUtcMidnight(birthDate); const end = toUtcMidnight(referenceDate); // 3) Prevent negative intervals if (start > end) { return '0 days'; } // 4) Calculate full years let years = end.getUTCFullYear() - start.getUTCFullYear(); // “Anniversary” = birthday + years let anniversary = new Date(Date.UTC( start.getUTCFullYear() + years, start.getUTCMonth(), start.getUTCDate() )); if (anniversary > end) { years--; anniversary = new Date(Date.UTC( start.getUTCFullYear() + years, start.getUTCMonth(), start.getUTCDate() )); } // 5) Calculate full months since last anniversary let temp = new Date(anniversary); let months = 0; while (true) { const next = new Date(Date.UTC( temp.getUTCFullYear(), temp.getUTCMonth() + 1, temp.getUTCDate() )); if (next <= end) { months++; temp = next; } else { break; } } // 6) Calculate remaining days → weeks + days const msPerDay = 24 * 60 * 60 * 1000; const daysTotal = Math.floor((end.getTime() - temp.getTime()) / msPerDay); const weeks = Math.floor(daysTotal / 7); const days = daysTotal % 7; // 7) Build the output, skipping zero-valued units const parts: string[] = []; if (years) parts.push(`${years} year${years > 1 ? 's' : ''}`); if (months) parts.push(`${months} month${months > 1 ? 's' : ''}`); if (weeks) parts.push(`${weeks} week${weeks > 1 ? 's' : ''}`); if (days) parts.push(`${days} day${days > 1 ? 's' : ''}`); // 8) Fallback if (parts.length === 0) { return '0 days'; } return parts.join(', '); }