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

Adding a month to a date removes an hour. #2068

Open
dennisholmer opened this issue Sep 22, 2022 · 1 comment
Open

Adding a month to a date removes an hour. #2068

dennisholmer opened this issue Sep 22, 2022 · 1 comment

Comments

@dennisholmer
Copy link

Describe the bug
Adding a month to a date removes an hour.

Expected behavior

const initialDate = dayjs('2022-02-28T00:00:00.000Z');
  const oneMonthAdded = initialDate.add(1, 'month');
  console.log(oneMonthAdded.toISOString());

Should output 2022-03-28T00:00:00.000Z

Actual behaviour
Outputs 2022-03-27T23:00:00.000Z

Information

  • Day.js Version: 1.10.7
  • OS: Win 10
  • Browser: N/A
  • Time zone: GMT+2
@mp3000mp
Copy link

Hi, I think this is the expected behaviour.

Some timezone does not have the same UTC all year.

For example in France we're UTC+2 from March to Octobre and UTC+1 from Novembre to February.

You can see that the behavior is the same with Day.js, Moment.js and native Date().
Please note that you can use the timezone plugin in order to take into account the time change when you format your Day.js object.

// dayjs
const d = dayjs('2022-02-28T00:00:00.000Z')
const d2 = d.add(1, 'month')
// moment
const m = moment('2022-02-28T00:00:00.000Z')
const m2 = moment('2022-02-28T00:00:00.000Z')
m2.add(1, 'month')
// native
const j = new Date(2022, 1, 28, 0, 0, 0)
const j2 = new Date(2022, 1, 28, 0, 0, 0)
j2.setMonth(j2.getMonth() + 1)

// one hour difference in relation to UTC
console.log(d.toISOString())
console.log(d2.toISOString())
// same behavior with moment
console.log(m.toISOString())
console.log(m2.toISOString())
// same behavior with native js Date object
console.log(j.toISOString())
console.log(j2.toISOString())

// we can see the explanation here because the utcOffset is different
console.log(d.utcOffset())
console.log(d2.utcOffset())
console.log(j.getTimezoneOffset())
console.log(j2.getTimezoneOffset())

// the timezone plugin allows us to display the date in a consistant way regarding to the user timezone
console.log(d.tz('Europe/Paris').format())
console.log(d2.tz('Europe/Paris').format())

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

2 participants