question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

format dayjs utc object with current timezone

See original GitHub issue

Describe the bug I want to format UTC date (comes from backend) with current timezone. Can’t find a way to do it. But fromNow formats a relative string, keeping in mind current timezone.

Expected behavior It’s a bit inconsistent that relative formatting of UTC date respects current timezone and absolute doesn’t.

The only problem here is to format utc time with current timezone. I can’t find any flag such as respectTimezone, so absolute date is formatted relative to current timezone.

Information

  • Day.js Version 1.9.7
  • OS: Xubuntu
  • Browser Chromium Version 87.0.4280.88 (Official Build) snap (64-bit), Firefox 84.0
  • Time zone: GMT+0300 (Moscow Standard Time)

code:


dayjs.extend(dayjs_plugin_relativeTime)
dayjs.extend(dayjs_plugin_localizedFormat)
dayjs.extend(dayjs_plugin_utc)
dayjs.extend(dayjs_plugin_timezone)

console.log(String(new Date()))

const now = dayjs()
console.log('now:', now.format())

const date = dayjs.utc("2020-12-20T23:30:00.000")
console.log('UTC date:', date.format())
console.log('UTC date format LLL:', date.format('LLL'))
console.log('UTC date from now:', date.fromNow())

output:

"Mon Dec 21 2020 02:45:43 GMT+0300 (Moscow Standard Time)"
"now:" "2020-12-21T02:45:43+03:00"
"UTC date:" "2020-12-20T23:30:00Z"
"UTC date format LLL:" "December 20, 2020 11:30 PM"
"UTC date from now:" "16 minutes ago"

Pen: https://codepen.io/mahenzon/pen/NWRvdYV?editors=0011

Screenshot: image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
BePo65commented, May 31, 2022

At the moment I am scanning the open parsing issues and stumble over formatting issues too. So I came to this issue,where I am not completely sure that I understand the problem. My interpretation is that the question is, how to format an utc date value to a defined time zone.

My tests don’t show a problem here.

Besides that I am trying to help improve the reaction to issues (if @iamkun is interested in that kind of help 😃 There are a lot of issues that could be closed, as the are answered.

As far as I understand, dayjs main goal is to be a very small replacement for moment, even if that means that not ever special case is covered. And in my opinion it is very good at that. So perhaps it is worth retrying a switch over from moment 😀

4reactions
cigzigwoncommented, Jul 1, 2021

What’s going on with TZ offsets? Confirmed. Same issue here. Makes switching over from moment a pain…

Read more comments on GitHub >

github_iconTop Results From Across the Web

UTC
Use UTC time .utc(). Returns a cloned Dayjs object with a flag to use UTC time. Use local time .
Read more >
day.js is not converting UTC to local time - Stack Overflow
In moment, I converted utc to local time via moment. utc('2020-04-22T14:56:09.388842'). local(). format('MM/DD/YY h:mm A') which returns 04/22/ ...
Read more >
How to use the dayjs.utc function in dayjs - Snyk
dateFormat(date, format, localize = true) { if (!localize) { return dayjs.utc(date).format(format); } return dayjs.utc(date).
Read more >
UTC · Day.js
This returns a Day.js object with a flag to use UTC time. ... Passing true will change the time zone without changing the...
Read more >
Docs - Moment.js
It offers functions that will convert to and from the system's local time zone, but it is always UTC internally. Unlike a Moment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found