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

The values ​​of dayjs().unix() and dayjs().utc().unix are the same, but dayjs().format('YYYY-MM-DD HH:mm:ss') and dayjs().utc().format('YYYY-MM-DD HH:mm:ss') are different #2710

Open
XiaoXiaoWu98 opened this issue Aug 16, 2024 · 3 comments

Comments

@XiaoXiaoWu98
Copy link

Describe the bug
The values ​​of dayjs().unix() and dayjs().utc().unix are the same, but dayjs().format('YYYY-MM-DD HH:mm:ss') and dayjs().utc().format('YYYY-MM-DD HH:mm:ss') are different

Expected behavior

The values ​​of dayjs().unix() and dayjs().utc().unix should be different

Information

  • Day.js Version [e.g. v1.0.0]
  • OS: [e.g. iOS]
  • Browser [e.g. chrome 62]
  • Time zone: [e.g. GMT-07:00 DST (Pacific Daylight Time)]
@potter-man
Copy link

Unix time should not be different. After the format you see different "numbers" but omit timezone. In fact it's actually the same date local zone and UTC.

@XiaoXiaoWu98
Copy link
Author

Unix time should not be different. After the format you see different "numbers" but omit timezone. In fact it's actually the same date local zone and UTC.

But if I want to get the UTC timestamp, I can only do it through dayjs().utc().format('YYYY-MM-DD HH:mm:ss').unix(), not directly dayjs().utc().unix(). I think the timestamp should be consistent with the time format after format.

@potter-man
Copy link

potter-man commented Aug 30, 2024

If you need unix timestamp (seconds) you don't need to format, just call .unix() on dayjs instance

dayjs().unix()                                             // 1725008613
dayjs.utc().unix()                                         // 1725008613
dayjs('2024-08-30T00:00:00Z').unix()                       // 1724976000
dayjs.utc('2024-08-30T00:00:00Z').unix()                   // 1724976000

As I mentioned you just lose information about zone and dayjs parses string in local zone

dayjs(dayjs().format('YYYY-MM-DD HH:mm:ss')).unix()        // 1725008613
dayjs(dayjs().utc().format('YYYY-MM-DD HH:mm:ss')).unix()  // 1724997813 - Z is lost

Default format includes zone

dayjs(dayjs().format()).unix()                             // 1725008613
dayjs(dayjs().utc().format()).unix()                       // 1725008613

Or you can add Z to your custom format

dayjs(dayjs().format('YYYY-MM-DD HH:mm:ssZ')).unix()       // 1725008613
dayjs(dayjs().utc().format('YYYY-MM-DD HH:mm:ssZ')).unix() // 1725008613

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