Dayjs: Doesn't add day

Created on 6 Sep 2018  路  6Comments  路  Source: iamkun/dayjs

Version:1.7.5
Wasn't reproducible on version 1.6.x (don't remember now)
Similar issue to: https://github.com/iamkun/dayjs/issues/262

Trying to add day to 28 of October 2018 doesn't add anything:
https://codepen.io/lexswed/pen/NLaRZY

console.log(dayjs('2018-10-28').format('MMM dd DD'))
console.log(dayjs('2018-10-28').add(1, 'day').format('MMM dd DD'))

image

All 6 comments

@LexSwed I can't reproduce this issue
image

I'm getting this exact same issue. Maybe has something to do with daylight savings. From googling "sunday 28th october clocks":

In the UK the clocks go forward 1 hour at 1am on the last Sunday in March, and back 1 hour at 2am on the last Sunday in October. The period when the clocks are 1 hour ahead is called British Summer Time (BST). There's more daylight in the evenings and less in the mornings (sometimes called Daylight Saving Time).

Can confirm that this bug occurs every year on last Sunday of October. And I'd guess that same error would occur on the last Sunday of march if subtracting a day from the first Monday of March.

The following is a very hackie solution. But it works for now until this is fixed.

  const dtm_ = dtm.clone(); 
    dtm = nkFn.dtm.trimDate(dtm.add(1, "day"));
    if (dtm_.isSame(dtm)) {

      dtm = nkFn.dtm.trimDate(dtm_.add(2, "day"));  // forces going to the next day

    }

nkFn.dtm.trimDate is just a custom function to make hours/mins/seconds = 0

@LexSwed, you need to consider the fact, that dayjs works in the local time zone by default, just like Date. There are PRs #168 and #326 to introduce a "UTC mode" which will allow work with an apparent "date-only" value, which you use in your case.

Your case is similar to #262, #249 and #74.

When you constructed the dayjs instance like this:

dayjs('2018-10-28')

you assumed, that it works like a "date-only" instance. After adding 1 day, you expected it to become like dayjs('2018-10-28'). However, it was wrong. Both dayjs and Date assumed, that you supplied a string in UTC and you just omitted the time part. You actually performed the following initialization:

dayjs('2018-10-28T00:00:00Z')

It means, that you have a full date+time object. Both dayjs and Date work in the local time zone by default, which you can confirm by using getters, setters and toString. The actually stored value will be this in Central Europe:

2018-10-28 01:00:00 GMT+01:00 (CET)

Depending on DST changes in your local time zone, adding or subtracting a day may result in an hour shift, which may affect your day value. If you want to prevent it, you need to use setters with UTC in their name, when working with Date. When working with dayjs you can do nothing, because there are no UTC-setters there yet. You workaround will work, or you can try the UTC mode from the PRs.

I like dayjs quite a bit. But had to move to Luxon to feel confident there wasn't something buggie going on every year on last Sunday of October.

@nukuuk @LexSwed You may could try v1.7.7 and check if this issue is fixed in your timezone. THX.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sunrise1970 picture Sunrise1970  路  4Comments

antony picture antony  路  5Comments

Newbie012 picture Newbie012  路  4Comments

vaquel picture vaquel  路  3Comments

oliv9286 picture oliv9286  路  4Comments