fix: use Ecuador timezone (GMT-5) for all dates
This commit is contained in:
+125
@@ -0,0 +1,125 @@
|
||||
// Date utilities for La Huasca - Ecuador is GMT-5 (no DST)
|
||||
|
||||
/**
|
||||
* Get current date/time in Ecuador timezone (GMT-5)
|
||||
*/
|
||||
export function getEcuadorDate(): Date {
|
||||
const now = new Date();
|
||||
// Ecuador is UTC-5, so subtract 5 hours from UTC
|
||||
const ecuadorOffset = -5 * 60; // minutes
|
||||
const utcOffset = now.getTimezoneOffset(); // local offset from UTC
|
||||
const ecuadorTime = new Date(now.getTime() + (utcOffset + ecuadorOffset) * 60000);
|
||||
return ecuadorTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get today's date in Ecuador (YYYY-MM-DD format)
|
||||
*/
|
||||
export function getEcuadorToday(): string {
|
||||
return formatDateEcuador(getEcuadorDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a date as YYYY-MM-DD in Ecuador timezone
|
||||
*/
|
||||
export function formatDateEcuador(date: Date): string {
|
||||
const ecuadorOffset = -5 * 60; // minutes
|
||||
const utc = date.getTime() + date.getTimezoneOffset() * 60000;
|
||||
const ecuadorTime = new Date(utc + ecuadorOffset * 60000);
|
||||
|
||||
const year = ecuadorTime.getFullYear();
|
||||
const month = String(ecuadorTime.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(ecuadorTime.getDate()).padStart(2, '0');
|
||||
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a date N days from today in Ecuador timezone
|
||||
*/
|
||||
export function getEcuadorDateFromToday(days: number): string {
|
||||
const today = getEcuadorDate();
|
||||
today.setDate(today.getDate() + days);
|
||||
return formatDateEcuador(today);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a date string (YYYY-MM-DD or ISO) for display in Ecuador timezone
|
||||
*/
|
||||
export function formatDisplayDate(dateStr: string, options?: Intl.DateTimeFormatOptions): string {
|
||||
const date = parseDateAsEcuador(dateStr);
|
||||
const defaultOptions: Intl.DateTimeFormatOptions = {
|
||||
weekday: 'short',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
timeZone: 'America/Guayaquil',
|
||||
};
|
||||
return date.toLocaleDateString('en-US', options || defaultOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a date with full weekday name
|
||||
*/
|
||||
export function formatDisplayDateFull(dateStr: string): string {
|
||||
return formatDisplayDate(dateStr, {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
timeZone: 'America/Guayaquil',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a datetime string for display
|
||||
*/
|
||||
export function formatDisplayDateTime(dateStr: string): string {
|
||||
const date = new Date(dateStr);
|
||||
return date.toLocaleString('en-US', {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
timeZone: 'America/Guayaquil',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a date string (YYYY-MM-DD) as Ecuador date, avoiding timezone shifts
|
||||
*/
|
||||
export function parseDateAsEcuador(dateStr: string): Date {
|
||||
// Parse as local components to avoid timezone shifts
|
||||
const parts = dateStr.split('T')[0].split('-');
|
||||
const year = parseInt(parts[0], 10);
|
||||
const month = parseInt(parts[1], 10) - 1; // JS months are 0-indexed
|
||||
const day = parseInt(parts[2], 10);
|
||||
return new Date(year, month, day);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate days until a date (in Ecuador timezone)
|
||||
*/
|
||||
export function daysUntilEcuador(dateStr: string): string {
|
||||
const today = getEcuadorDate();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
const target = parseDateAsEcuador(dateStr);
|
||||
target.setHours(0, 0, 0, 0);
|
||||
const diff = Math.ceil((target.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (isNaN(diff)) return 'Unknown';
|
||||
if (diff === 0) return 'Today';
|
||||
if (diff === 1) return 'Tomorrow';
|
||||
if (diff < 0) return 'Past';
|
||||
return `in ${diff} days`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get month and day for display (e.g., "Jun 29")
|
||||
*/
|
||||
export function getMonthDayEcuador(dateStr: string): { month: string; day: number } {
|
||||
const date = parseDateAsEcuador(dateStr);
|
||||
return {
|
||||
month: date.toLocaleString('en-US', { month: 'short' }),
|
||||
day: date.getDate(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user