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

Creating a UTC object from a dayjs object fails #1241

Open
JuanMME opened this issue Nov 26, 2020 · 2 comments
Open

Creating a UTC object from a dayjs object fails #1241

JuanMME opened this issue Nov 26, 2020 · 2 comments
Labels
☢️Bug Something isn't working

Comments

@JuanMME
Copy link

JuanMME commented Nov 26, 2020

Describe the bug
Do this:

let plainDate = '2018-02-20T23:59:59.000Z';
let obj = dayjs(plainDate);
console.log(1, dayjs.utc(plainDate).toISOString());
console.log(1, dayjs.utc(obj).toISOString());
console.log(2, dayjs(plainDate).utc().toISOString());
console.log(2, dayjs(obj).utc().toISOString());

The second console.log will print an incorrect date (today at 00:00:00) instead of the original date passed. Doing this however:

console.log(1, dayjs.utc(obj.toDate()).toISOString());

Seems to work just fine.

Expected behavior
Creating UTC Dayjs objects should work the same way as creating regular dayjs objects, which means passing a dayjs object as a parameter should work just fine. Instead, creating a dayjs object from a dayjs object works only if you do dayjs() but not dayjs.utc().

Information

  • Day.js Version 1.9.6
  • OS: Windows 10
  • Browser Edge 87
  • Time zone: CET
@iamkun iamkun added the ☢️Bug Something isn't working label Nov 26, 2020
@iamkun
Copy link
Owner

iamkun commented Nov 26, 2020

Bug confirmed. dayjs.utc(Dayjs) should have the same behavior as dayjs(Dayjs)

@rmartins90
Copy link

rmartins90 commented Mar 7, 2023

Tried to reproducte this behavior in the current version and this seems to solved now. Below, you can find the test used to validate this scenario.

Wasn't able to run over all the tests to verify that this scenario is covered already. So, no PR was created, but it should be closed.

  const dayjsDate = dayjs()
  const daysjsUTCDate = dayjs.utc()

  expect(dayjsDate.toISOString()).toBe(daysjsUTCDate.toISOString())

  const plainDate = '2018-02-20T23:59:59.000Z'
  const obj = dayjs(plainDate)

  const result1 = dayjs.utc(plainDate).toISOString()
  const result2 = dayjs.utc(obj).toISOString()
  const result3 = dayjs(plainDate).utc().toISOString()
  const result4 = dayjs(obj).utc().toISOString()

  expect(result1).toEqual(result2)
  expect(result2).toEqual(result3)
  expect(result3).toEqual(result4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants