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

TimeZone issue on Dayjs-values initially created before DST #2623

Open
anderskjeldsen opened this issue Apr 5, 2024 · 0 comments
Open

TimeZone issue on Dayjs-values initially created before DST #2623

anderskjeldsen opened this issue Apr 5, 2024 · 0 comments

Comments

@anderskjeldsen
Copy link

** Description of the problem **
Running the current typescript code:

printDates() {
var timeAfterDaylightSavings = new Date(2024, 2, 31, 18, 0, 0, 0);
var startOfDayWithDST = dayjs.tz(timeAfterDaylightSavings, "Europe/Oslo").startOf('day');
var currentTime = startOfDayWithDST;
for(let i = 0; i < 4; i++) {
this.printDate(currentTime);
currentTime = currentTime.add(1, 'hour');
}
}

printDate(d: dayjs.Dayjs) {
return console.log("Date: " + d.format('YYYY-MM-DD HH:mm:ss') + ", UTC date: " + d.toDate().toISOString());
}

It prints the following:

TZ date: 2024-03-31 00:00:00, UTC date: 2024-03-30T23:00:00.000Z
TZ date: 2024-03-31 01:00:00, UTC date: 2024-03-31T00:00:00.000Z
TZ date: 2024-03-31 03:00:00, UTC date: 2024-03-31T02:00:00.000Z <--- wrong
TZ date: 2024-03-31 04:00:00, UTC date: 2024-03-31T03:00:00.000Z

The third line is wrong. DST happens at 02:00 local time and therefore local time jumps from 1:59 to 3. This is correct. However, UTC time should not skip an extra hour as well.

Expected behavior
I write a similar snippet using Java and ZonedDateTime, and it appears to print the correct values (not the same date formats):
TZ date: 2024-03-31T00:00+01:00[Europe/Oslo], UTC date: 30 Mar 2024 23:00:00 GMT
TZ date: 2024-03-31T01:00+01:00[Europe/Oslo], UTC date: 31 Mar 2024 00:00:00 GMT
TZ date: 2024-03-31T03:00+02:00[Europe/Oslo], UTC date: 31 Mar 2024 01:00:00 GMT
TZ date: 2024-03-31T04:00+02:00[Europe/Oslo], UTC date: 31 Mar 2024 02:00:00 GMT

Also, doing it the other way around by adding hours to the Date objects and creating a new Dayjs object from that everytime works (append the following code inside the printDates function):
let currentDate = startOfDayWithDST.toDate();
for(let i = 0; i < 4; i++) {
currentTime = dayjs.tz(currentDate, "Europe/Oslo");
this.printDate(currentTime);
currentDate.setHours(currentDate.getHours() + 1)
}

TZ Date: 2024-03-31 00:00:00, UTC date: 2024-03-30T23:00:00.000Z
TZ Date: 2024-03-31 01:00:00, UTC date: 2024-03-31T00:00:00.000Z
TZ Date: 2024-03-31 03:00:00, UTC date: 2024-03-31T01:00:00.000Z
TZ Date: 2024-03-31 04:00:00, UTC date: 2024-03-31T02:00:00.000Z

Information

  • Day.js Version: v1.11.10
  • OS: Any (mostly tested on Ubuntu and Mac)
  • Browser: Chrome (probably others)
  • Time zone: Europe/Oslo
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

1 participant