Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subDays adds one hour arround DST #3750

Open
atlanteh opened this issue Mar 31, 2024 · 1 comment
Open

subDays adds one hour arround DST #3750

atlanteh opened this issue Mar 31, 2024 · 1 comment

Comments

@atlanteh
Copy link

Using latest version (3.6.0) for removing days

const date = new Date('2024-03-29T00:00:00.000Z')
console.log(date.toISOString(), date.getTimezoneOffset())
// '2024-03-29T00:00:00.000Z', -180

Which is Israel Daylight Time

const date = subDays(new Date('2024-03-29T00:00:00.000Z'), 1)
console.log(date.toISOString(), date.getTimezoneOffset())
// '2024-03-28T01:00:00.000Z', -120

Which is Israel Standard Time

According to #1302 (comment) adding/removing days are supposed to respect timezone changes

Am I missing something?

@fturmel
Copy link
Member

fturmel commented Mar 31, 2024

I believe it's working as intended. Your initial datetime is 3AM the 29th in IDT (midnight UTC), and when doing the minus one day operation we land on the 28th at 3AM IST.

Regarding your linked issue, 24 hours !== 1 day during a daylight saving transition. If you substract 24 hours from your date, you will land on the 28th at 2AM IST.

// process.env.TZ = 'Asia/Jerusalem';

import { subDays, subHours } from 'date-fns';

const date = new Date('2024-03-29T00:00:00.000Z');
const dateMinusOneDay = subDays(date, 1);
const dateMinus24Hours = subHours(date, 24);

console.log(date.toISOString(), date.getTimezoneOffset()); // 2024-03-29T00:00:00.000Z -180
console.log(date.toString()); // Fri Mar 29 2024 03:00:00 GMT+0300 (Israel Daylight Time)

console.log(dateMinusOneDay.toISOString(), dateMinusOneDay.getTimezoneOffset()); // 2024-03-28T01:00:00.000Z -120
console.log(dateMinusOneDay.toString()); // Thu Mar 28 2024 03:00:00 GMT+0200 (Israel Standard Time)

console.log(dateMinus24Hours.toISOString(), dateMinus24Hours.getTimezoneOffset()); // 2024-03-28T00:00:00.000Z -120
console.log(dateMinus24Hours.toString()); // Thu Mar 28 2024 02:00:00 GMT+0200 (Israel Standard Time)

Bonus: if you wanted the operation to happen in UTC instead, have a look at the UTCDate companion library.

Hope this clarified things!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants