iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
47k stars 2.3k forks source link

.add(1, 'month) also changes the day, not just the month #2564

Open henriqueleite42 opened 9 months ago

henriqueleite42 commented 9 months ago

Describe the bug Day is changed to the first day of the month

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

dayjs.extend(utc);
dayjs.extend(timezone);

dayjs.tz.setDefault('UTC');

dayjs.tz('2024-01-15').add(1, 'month').toISOString()

// Expected: '2024-02-15T00:00:00.000Z'
// Actual result: '2024-02-01T00:00:00.000Z'

Information

henriqueleite42 commented 9 months ago

Sorry, my mistake, I was seeing the wrong result

henriqueleite42 commented 9 months ago

Actually, I don't believe that it's my mistake, the bug is really happening.

buza-me commented 9 months ago

You have a comma in your example. I wonder if you have add() defined somewhere. I just tested it out of curiosity in Node 20 on Windows and it works as expected.

henriqueleite42 commented 9 months ago

Sorry, the comma was a typo that only exists in this example, but not in the real scenario where the bug happens.

thadeucity commented 8 months ago

The bug is real and it happens for .add(1, 'year') too

const initial = dayjs.tz('2024-02-15').format('YYYY-MM-DD') // 2024-02-15
const expected= dayjs('2024-02-15').add(1, 'year').format('YYYY-MM-DD') // 2025-02-15
const result = dayjs.tz('2024-02-15').add(1, 'year').format('YYYY-MM-DD') // 2025-02-01

The bug only affects the day

EDIT: The bug only happens when timezone is set to UTC EDIT2: Looks like it is a problem with: .endOf('month') when dealing with UTC timezone EDIT3: I believe it has something to do with the fact that UTC does not returns an offset (since it is ZERO), I reproduced the same error with Europe/Lisbon that has a ZERO offset too