iamkun / iamkun/dayjs

format dayjs utc object with current timezone

Open
#1,284 11 comments 9 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**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:
```js

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](https://user-images.githubusercontent.com/20804158/102727447-bcd06100-4336-11eb-9ab5-d8f11c6c7959.png)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the provided dayjs.utc example and the relativeTime, localizedFormat, utc, and timezone plugins. Compare the absolute format output with fromNow in the reported GMT+0300 timezone; done means the intended current-timezone behavior is defined and covered for UTC input.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
localization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.