Giter VIP home page Giter VIP logo

Comments (3)

fturmel avatar fturmel commented on June 24, 2024

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 console.log() outputs?

This might overlap a daylight saving transition, but I can't think of a reason why the dates wouldn't match up.

from date-fns.

77it avatar 77it commented on June 24, 2024

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 node --experimental-network-imports test.mjs

date_20170331_plus1_plus1: Mon Apr 02 1917 01:00:00 GMT+0200 (Ora legale dell’Europa centrale)
date_20170402: Mon Apr 02 1917 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
test #1 failed

Test run with Deno deno run test.mjs

date_20170331_plus1_plus1: Mon Apr 02 1917 01:00:00 GMT+0200 (Ora legale dell’Europa centrale)
date_20170402: Mon Apr 02 1917 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
test #1 failed

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:

new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount)

PS
I updated the code posted in the issue to add the import statement.

from date-fns.

fturmel avatar fturmel commented on June 24, 2024

Thanks for the additional details. Here's what's happening:

new Date(1917, 3, 1) is actually Sun Apr 01 1917 01:00:00 GMT+0200 (Central European Summer Time) because it is exactly during a daylight saving transition and midnight technically doesn't exist.

new Date(1917, 3, 2) is Mon Apr 02 1917 00:00:00 GMT+0200 (Central European Summer Time) as expected.

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

from date-fns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.