fix - timezone fix2

This commit is contained in:
2025-09-20 21:18:52 +05:30
parent 2068b6b859
commit b7d2289576

View File

@@ -122,41 +122,30 @@ export function formatDateToHumanReadable(dateInput?: string | Date): string {
// date-only string -> show as-is // date-only string -> show as-is
if (typeof dateInput === "string" && isDateOnlyString(dateInput)) { if (typeof dateInput === "string" && isDateOnlyString(dateInput)) {
const parts = dateInput.split("-"); const [y, m, d] = dateInput.split("-");
const [y, m, d] = parts; if (!y || !m || !d) return "Invalid Date";
return `${MONTH_SHORT[parseInt(m, 10) - 1]} ${d}, ${y}`;
// Defensive check so TypeScript and runtime are both happy
if (!y || !m || !d) {
console.error("Invalid date-only string:", dateInput);
return "Invalid Date";
} }
const day = d.padStart(2, "0"); // Handle Date object
const monthIndex = parseInt(m, 10) - 1;
const monthName = MONTH_SHORT[monthIndex] ?? "Invalid";
return `${day} ${monthName} ${y}`;
}
// Date object -> use its calendar fields (no TZ conversion)
if (dateInput instanceof Date) { if (dateInput instanceof Date) {
if (isNaN(dateInput.getTime())) return "Invalid Date"; if (isNaN(dateInput.getTime())) return "Invalid Date";
const dd = String(dateInput.getDate()).padStart(2, "0"); const dd = String(dateInput.getDate());
const mm = MONTH_SHORT[dateInput.getMonth()]; const mm = MONTH_SHORT[dateInput.getMonth()];
const yy = dateInput.getFullYear(); const yy = dateInput.getFullYear();
return `${dd} ${mm} ${yy}`; return `${mm} ${dd}, ${yy}`;
} }
// Otherwise (ISO/timestamp string) -> parse and use UTC date components // Handle ISO/timestamp string (UTC-based to avoid shifting)
const parsed = new Date(dateInput); const parsed = new Date(dateInput);
if (isNaN(parsed.getTime())) { if (isNaN(parsed.getTime())) {
console.error("Invalid date input provided:", dateInput); console.error("Invalid date input provided:", dateInput);
return "Invalid Date"; return "Invalid Date";
} }
const dd = String(parsed.getUTCDate()).padStart(2, "0"); const dd = String(parsed.getUTCDate());
const mm = MONTH_SHORT[parsed.getUTCMonth()]; const mm = MONTH_SHORT[parsed.getUTCMonth()];
const yy = parsed.getUTCFullYear(); const yy = parsed.getUTCFullYear();
return `${dd} ${mm} ${yy}`; return `${mm} ${dd}, ${yy}`;
} }
/** /**