question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add days across Daylight Saving Time

See original GitHub issue

When adding days to a time, it’s assuming each day is 24 hours long. This is not a correct assumption when crossing over daylight saving time.

Steps to recreate

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import tz from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(tz);

// 2020-10-25 is when DST starts in Europe/London (clocks go back by one hour). 
// This means that 2020-10-25 is a day that's only 23 hours long
const date1 = dayjs.tz('2020-10-24', 'Europe/London').add(2, 'day').toISOString();
const date2 = dayjs.tz('2020-10-26', 'Europe/London').toISOString();

// Prints
// {
//   date1: '2020-10-25T23:00:00.000Z',
//   date2: '2020-10-26T00:00:00.000Z'
// }
console.log({ date1, date2 });

Expected behavior date1 and date2 should be the same, as adding the number of “days” to the date should’ve allowed for the fact that not all days are 24 hours.

Information

  • Day.js Version: v1.9.2
  • OS: Windows 10 20H2
  • Browser: Chromium: 87.0.4280.101
  • Time zone: (UTC -04:00) Santiago

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:8

github_iconTop GitHub Comments

6reactions
ChristianKlimacommented, Aug 30, 2021

We completely switched our project to dayjs and at the end noticed that dayjs does not support the daylight saving time. I can only warn against using this library. Operations such as add cause unexpected results. As soon as a calculation goes over the limit of the time change, the result is wrong. We will continue to use moment.js. This library can only be used in countries without daylight saving time. Example:

dayjs(‘25-10-2020’, ‘DD-MM-YYYY’) .tz(‘Europe/Berlin’) .add(1, ‘day’) .diff(dayjs(‘26-10-2020’, ‘DD-MM-YYYY’).tz(‘Europe/Berlin’), ‘days’) .toString()

result = -3600000

3reactions
moulinraphaelcommented, Feb 3, 2021

This is an important issue for us, but there is a workaround possible…

  1. Get the date’s TZ
  2. Add days using date method
  3. Use tz method to “reset” TZ (date method doesn’t update the offset value)
export const addDaysExtended = (dateWithTZ, days) => {
  const clone = dayjs(dateWithTZ);
  const tz = clone.$x.$timezone;
  return clone.date(clone.date() + days).tz(tz, true);
};

const date1 = dayjs.tz('2020-10-24', 'Europe/London').add(2, 'day').toISOString();
const date2 = dayjs.tz('2020-10-26', 'Europe/London').toISOString();
const date3 = addDaysExtended(dayjs.tz('2020-10-24', 'Europe/London'), 2).toISOString();

console.log({ date1, date2, date3 });
// {
//    date1: '2020-10-25T23:00:00.000Z',
//    date2: '2020-10-26T00:00:00.000Z',
//    date3: '2020-10-26T00:00:00.000Z'
// }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Moment.js issue adding days with Daylight Savings Time
Moment.js issue adding days with Daylight Savings Time · 1.I create a moment date: var m = moment(new Date(2014, 9, 18, 0, 0,...
Read more >
daylight savings, addDays and addToTime
Hello, I have a power app, which creates events with the possibility to automatically create follow ups on a regular basis, ...
Read more >
Daylight Saving Time 2022: When Does the Time Change?
Daylight Saving Time ends on Sunday, November 6, 2022, at 2:00 A.M. On Saturday night, clocks are set back one hour (i.e., gaining...
Read more >
Daylight Saving Time 2023: When Does the Time Change?
The first Sunday in November is when Daylight Saving Time ends in most areas of the U.S., so in 2023 we “fall back”...
Read more >
How Does Daylight Saving Time Work? - Time and Date
In the fall (autumn), the DST period usually ends, and our clocks are set back to standard time again. In terms of hours...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found