date-and-time icon indicating copy to clipboard operation
date-and-time copied to clipboard

Addition in a specific time zone

Open alex996 opened this issue 1 year ago • 0 comments

Thank you for your library. I have a suggestion. I see that in #72 you added utc?: boolean | undefined parameter. This is great, because for example, it makes addHours(dateObj, hours, true) idempotent no matter what the system time zone is. But it would be more flexible to pass any IANA time zone:

-export function addHours(dateObj: Date, hours: number, utc?: boolean): Date;
+export function addHours(dateObj: Date, hours: number, timeZone?: string): Date;

Or a new function in the timezone plugin:

+export function addHoursTZ(dateObj: Date, hours: number, timeZone?: string): Date;

Here is the example I was testing:

// In the US, the clocks fall back on Nov 3, 2024 at 2:00 am
const mar10 = date.parseTZ("2024-03-10 00:00", "YYYY-MM-DD HH:mm", "America/Chicago"); // UTC-6
const mar11 = date.addDays(mar10, 1); // UTC-5

console.log(mar10.toISOString());
console.log(mar11.toISOString());
# When you run the code on a system in America/Chicago time zone, it's correct:
2024-03-10T06:00:00.000Z
2024-03-11T05:00:00.000Z

# But when you run it on a system that doesn't observe DST, such as America/Sao_Paulo:
2024-03-10T06:00:00.000Z
2024-03-11T06:00:00.000Z

I understand that this might be a big ask. For the time being, it might be useful to add a note in the readme, that add* functions adjust for DST based on the local time zone, not the date's time zone (unless the 3rd param is true, then DST is effectively ignored). In other words, if the user's TZ has different DST rules from the date's TZ, this will impact the math.

alex996 avatar Jun 13 '24 10:06 alex996