-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
bug with addDays()? #3767
Comments
I can't reproduce the problem. Which version of date-fns is this? Can you share your IANA time zone and which JS runtime you are using? What are the This might overlap a daylight saving transition, but I can't think of a reason why the dates wouldn't match up. |
I'm in Italy, zone Europe/Rome I tried with Nodejs version v21.6.2 and Deno version deno 1.41.0, same result. Test run with Nodejs
Test run with Deno
Same output running the code online with Chrome with https://playcode.io/1834998 For my needs I use the following code to add days; it ignores the time part and works with any day I've tried:
PS |
Thanks for the additional details. Here's what's happening:
So if you add a day to April 1st at 1AM, you get April 2nd at 1AM, not midnight. If you don't want to take into account local time zone and DST, you should use UTC instead, either through the UTCDate package : // process.env.TZ = 'Europe/Rome';
import { UTCDate } from '@date-fns/utc';
import { addDays } from 'date-fns';
const april1 = new UTCDate(1917, 3, 1);
const april2 = new UTCDate(1917, 3, 2);
const april1Plus1Day = addDays(april1, 1);
console.log(april1.toISOString());
console.log(april2.toISOString());
console.log(april1Plus1Day.toISOString());
// 1917-04-01T00:00:00.000Z
// 1917-04-02T00:00:00.000Z
// 1917-04-02T00:00:00.000Z ...or directly with the native JS Date for such a simple operation: // process.env.TZ = 'Europe/Rome';
const april1 = new Date(Date.UTC(1917, 3, 1));
const april2 = new Date(Date.UTC(1917, 3, 2));
const april1Plus1Day = new Date(april1);
april1Plus1Day.setUTCDate(april1.getUTCDate() + 1);
console.log(april1.toISOString());
console.log(april2.toISOString());
console.log(april1Plus1Day.toISOString());
// 1917-04-01T00:00:00.000Z
// 1917-04-02T00:00:00.000Z
// 1917-04-02T00:00:00.000Z |
When testing
addDays()
I found a strange behaviour: date 1917/04/01 + 1 day is not equal to 1917/04/02, that is wrong.Instead, the date 1917/03/31 + 2 days is equal to 1917/04/02, which is correct.
Follows the code
The text was updated successfully, but these errors were encountered: