mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 09:11:13 -05:00
Fix timezone-independent date display
This commit is contained in:
@@ -139,6 +139,33 @@ export function compareDateStrings(date1: string, date2: string): number {
|
||||
return date1 < date2 ? -1 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely parses a date value (string or Date) for display formatting
|
||||
* Ensures YYYY-MM-DD strings are interpreted as local dates, not UTC
|
||||
*
|
||||
* This prevents timezone bugs where "1972-10-01" would display as
|
||||
* "September 30, 1972" for users in negative UTC offset timezones.
|
||||
*
|
||||
* @param date - Date string (YYYY-MM-DD) or Date object
|
||||
* @returns Date object in local timezone
|
||||
*
|
||||
* @example
|
||||
* // User in UTC-8 viewing "1972-10-01"
|
||||
* parseDateForDisplay("1972-10-01"); // Returns Oct 1, 1972 00:00 PST ✅
|
||||
* // NOT Sep 30, 1972 16:00 PST (what new Date() would create)
|
||||
*/
|
||||
export function parseDateForDisplay(date: string | Date): Date {
|
||||
if (date instanceof Date) {
|
||||
return date;
|
||||
}
|
||||
// If it's a YYYY-MM-DD string, use parseDateOnly for local interpretation
|
||||
if (typeof date === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
||||
return parseDateOnly(date);
|
||||
}
|
||||
// Fallback for other date strings (timestamps, ISO strings, etc.)
|
||||
return new Date(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a date string for a specific precision
|
||||
* Sets the date to the first day of the period for month/year precision
|
||||
|
||||
Reference in New Issue
Block a user