fixed date issue v1

This commit is contained in:
2025-10-17 06:22:17 +05:30
parent a5d5e96d9a
commit 46f7b703f4

View File

@@ -1,11 +1,11 @@
/** /**
* Use parseLocalDate when you need a Date object at local midnight * Use parseLocalDate when you need a Date object at local midnight
* (for calendars, date pickers, Date math in the browser). * (for calendars, date pickers, Date math in the browser).
* *
* *
* Parse a date string in yyyy-MM-dd format (assumed local) into a JS Date object. * Parse a date string in yyyy-MM-dd format (assumed local) into a JS Date object.
* No timezone conversion is applied. Returns a Date at midnight local time. * No timezone conversion is applied. Returns a Date at midnight local time.
* *
* * Accepts: * * Accepts:
* - "YYYY-MM-DD" * - "YYYY-MM-DD"
* - ISO/timestamp string (will take left-of-'T' date portion) * - ISO/timestamp string (will take left-of-'T' date portion)
@@ -44,10 +44,10 @@ export function parseLocalDate(input: string | Date): Date {
} }
/** /**
* Use formatLocalDate when you need a date-only string "YYYY-MM-DD" (for displaying stable date values in UI lists, * Use formatLocalDate when you need a date-only string "YYYY-MM-DD" (for displaying stable date values in UI lists,
* sending to APIs, storing in sessionStorage/DB where date-only is required). * sending to APIs, storing in sessionStorage/DB where date-only is required).
* *
* *
* Format a date value into a "YYYY-MM-DD" string with **no timezone shifts**. * Format a date value into a "YYYY-MM-DD" string with **no timezone shifts**.
* *
* Handles all common input cases: * Handles all common input cases:
@@ -84,13 +84,23 @@ export function formatLocalDate(input?: string | Date): string {
if (input instanceof Date) { if (input instanceof Date) {
if (isNaN(input.getTime())) return ""; if (isNaN(input.getTime())) return "";
// ALWAYS use the local calendar fields for Date objects. // HYBRID LOGIC:
// This avoids day-flips when a Date was constructed from an ISO instant // - If this Date was likely created from an ISO instant at UTC midnight
// and the browser's timezone would otherwise show a different calendar day. // (e.g. "2025-10-15T00:00:00Z"), then getUTCHours() === 0 but getHours()
const y = input.getFullYear(); // will be non-zero in most non-UTC timezones. In that case use UTC date
const m = `${input.getMonth() + 1}`.padStart(2, "0"); // parts to preserve the original calendar day.
const d = `${input.getDate()}`.padStart(2, "0"); // - Otherwise use the local calendar fields (safe for local-midnight Dates).
return `${y}-${m}-${d}`; const utcHours = input.getUTCHours();
const localHours = input.getHours();
const useUTC = utcHours === 0 && localHours !== 0;
const year = useUTC ? input.getUTCFullYear() : input.getFullYear();
const month = useUTC ? input.getUTCMonth() + 1 : input.getMonth() + 1;
const day = useUTC ? input.getUTCDate() : input.getDate();
const m = `${month}`.padStart(2, "0");
const d = `${day}`.padStart(2, "0");
return `${year}-${m}-${d}`;
} }
return ""; return "";
@@ -166,7 +176,6 @@ export function formatDateToHumanReadable(dateInput?: string | Date): string {
return "Invalid Date"; return "Invalid Date";
} }
// ---------------- OCR Date helper -------------------------- // ---------------- OCR Date helper --------------------------
/** /**
* Convert any OCR numeric-ish value into a number. * Convert any OCR numeric-ish value into a number.