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

Incorrect .add() result with timezone plugin #2195

Closed
YiJing233 opened this issue Jan 9, 2023 · 12 comments
Closed

Incorrect .add() result with timezone plugin #2195

YiJing233 opened this issue Jan 9, 2023 · 12 comments

Comments

@YiJing233
Copy link

Describe the bug
Hi Team, there is a confusing problem I'm facing with:
There is a timestamp, and I transform it into a dayjs object with the timezone plugin. But when I use .add(1, 'month'), the result is wrong, which seems to be added too much😫.

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

const originalDate = dayjs.tz(1669852800000, "Etc/GMT");
console.log("originalDate", originalDate);
console.log('formated OriginalDate', originalDate.toISOString());
const addedDate = originalDate.add(1, "month");
console.log("addedDate", addedDate);
console.log('formated addedDate', addedDate.toISOString());

And the result is:

originalDate M {$L: "en", $d: Thu Dec 01 2022 00:00:00 GMT+0800 (中国标准时间), $x: Object, $y: 2022, $M: 11…}

formated OriginalDate 2022-12-01T00:00:00.000Z 

addedDate  M {$L: "en", $u: true, $offset: 0, $d: Tue Jan 31 2023 00:00:00 GMT+0800 (中国标准时间), $x: Object…}

formated addedDate 2023-01-31T00:00:00.000Z 

Here is my codesandbox link: https://codesandbox.io/s/dank-lake-kc5w1i?file=/src/index.js

Expected behavior
It should be '2022-01-01T00:00:00.000Z'

Information

  • Day.js Version v1.11.7
  • OS: MacOS Monterey 12.5.1
  • Browser Chrome 108.0.5359.124 (x86_64)
  • Time zone: [Asia/Shanghai / UTC +8]
@KronosDev-Pro
Copy link

KronosDev-Pro commented Jan 10, 2023

I suppose this is a GMT topic is in issue #2037 and solved in pull request #2118.

Test

describe('issue 2195 - add in UTC timezone', () => {
  it('Thu Dec 01 2022 00:00:00 GMT+0800, with "+ 1 month"', () => {
    dayjs.extend(utc)
    dayjs.extend(timezone)

    expect(dayjs(1669852800000).tz(GMT).add(1, 'month').format()).toBe('2023-01-01T00:00:00Z')
    expect(dayjs(1669852800000).tz(GMT).add(1, 'month').isSame(moment(1669852800000).tz(GMT).add(1, 'M'))).toBe(true)
  })
})
// pass

result : 2023-01-01T00:00:00.000Z

@YiJing233
Copy link
Author

YiJing233 commented Jan 10, 2023

Sorry that I can not get the same result as you did 😭. I cloned the repo and installed moment-timezone as Moment.js Doc said and then run this case, but the output of testing is failed.

// timezone.test.js
import MockDate from 'mockdate'
// import moment from 'moment'
import dayjs from '../src'
import timezone from '../src/plugin/timezone'
import utc from '../src/plugin/utc'
import moment from 'moment-timezone';
// ...
it('test issue #2195', () => {
  console.log(dayjs(1669852800000).tz('GMT').add(1, 'month'))
  expect(dayjs(1669852800000).tz('GMT').add(1, 'month').isSame(moment(1669852800000).tz('GMT').add(1, 'M'))).toBe(true);
})

And the result is

    Expected value to be:
      true
    Received:
      false
 Dayjs  object '$d': 2023-01-30T11:00:00.000Z,
 Moment Moment<2023-01-01T00:00:00Z>

Moment is the correct result.

I have debugged the running progress of .add() function, and I noticed that It happened when somewhere (maybe .clone()?) setting the $d value with .getUTCDate() which seems got '30',

@KronosDev-Pro
Copy link

You cloned this repo https://github.com/BePo65/dayjs/tree/fix/issue2037 ?

@KronosDev-Pro
Copy link

I updated the test above

here is the feedback:
image

@YiJing233
Copy link
Author

YiJing233 commented Jan 10, 2023

I cloned the repo itself dayjs and run the test cases in branch 'dev', here is my result:

截屏2023-01-10 23 13 18

And I found that when my machine timezone setting was UTC+8, the cases failed, but when I changed to UTC+0, cases passsed.
截屏2023-01-10 23 18 19

@KronosDev-Pro
Copy link

Okay, have you tried with this repo dayjs ? it fixes some timezone plugin errors

@KronosDev-Pro
Copy link

I have set the time zone on my machine to UTC+8.

With the dayjs repo, I have the same error
while on the 'fix/issue2037' repo, I have no errors

@YiJing233
Copy link
Author

YiJing233 commented Jan 10, 2023

Ok, I used the repo you said above and changed the branch to 'fix/issue2037' and the test case passed successfully.🎉

Thanks for your patience, and may I ask when will this branch be merged into main?

@KronosDev-Pro
Copy link

no worries, with pleasure.

unfortunately I don't have this information.

@KronosDev-Pro
Copy link

Hi 👋
@YiJing233 don't forget to close this issue, if is solve 😉

@zxd65885152
Copy link

Hi,
Same problem suffered.
I'd like to ask whether the pr is merged?

@KronosDev-Pro
Copy link

Hi 👋🏼
@zxd65885152 Unfortunately the fix has still not been merged and no sign of a possible merge on the horizon

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

3 participants