iamkun / iamkun/dayjs

Add days across Daylight Saving Time

Open
#1,271 18 comments 45 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

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**
```js
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.