Dayjs format with timezone abbreviation i.e. 'z' like in moment.js
Asked Answered
L

3

13

Trying to migrate from Moment.js to Dayjs but the only thing I can't get working is the Timezone abbreviation.

dayjs('2020-07-06T14:30:00.000-04:00').tz(dayjs.tz.guess()).format('Do MMM YYYY [at] HH:MMa z')

Calling the above I would expect 6th Jul 2020 at 08:07am EDT but currently I am just getting z where EDT is.

I have added the utc and timezone plugins and can't see any other plugins needed to make this work, I notice on Dayjs format docs that z isn't listed but searching the web, I see a lot of folks saying the solution is format('', {timeZone}) but the format doesn't take a second argument??

Looks like it was added to tz-plugin: https://github.com/iamkun/dayjs/pull/325/files#diff-cad460a5e46a2a48c59e95356c683820R195

Here's a code sandbox with an example of the issue: https://codesandbox.io/s/hungry-knuth-r58gz

--- Edit

Looks like support for tz abbr was removed :( https://github.com/iamkun/dayjs/pull/448/commits/e64c03cea79c579bcd3c4caf235b4724e56614d4

Lecithinase answered 20/9, 2020 at 8:22 Comment(0)
K
32

The z formatting option is added in 1.9.0 version of dayjs: https://github.com/iamkun/dayjs/pull/1069

Update the newest version and set up the plugins correctly, which should work. Example below:

var dayjs = require("dayjs")
var utc = require("dayjs/plugin/utc")
var timezone = require("dayjs/plugin/timezone")
var advanced = requires("dayjs/plugin/advancedFormat")

dayjs.extend(timezone)
dayjs.extend(utc)
dayjs.extend(advanced)
dayjs().tz('Europe/Paris').format('DD/MM/YYYY z')
Kuhl answered 21/10, 2020 at 18:53 Comment(6)
Oh nice they added support but opted to remove TZ name due to issues with confusion abbr. Using UTC everywhere nowLecithinase
Been looking for this for hours! Thanks for the simple solution - had everything in there except the dayjs.extend(advanced) --> no mention of that in the docs, so thanks!Polysyllabic
When I do this I get '08/06/2023 GMT+2'. How do I get the three letter abbreviation instead of the GMT+n format?Himalayas
@Himalayas exactly i am facing same problem. have you figured out the solution ?Litta
Hi @Bilal, I managed to convince our product team that we should stick to "GMT+1", instead of BST. The reason that dayjs has dropped the three letter abbreviation, is because it's known to cause confusion. For example, BST can mean "British Standard Time" in the UK, but in another locale it might mean "Bahraini Standard Time". There was a good, comprehensive StackOverflow answer on this, but I can't find it right now.Himalayas
Hi @Himalayas thanks for letting me know.. if you found that question please post here so other can also get benifitLitta
I
1

found this thread https://github.com/iamkun/dayjs/issues/1713 there are some workaround if you are interested, the one I might end up using is this

new Intl.DateTimeFormat('en-AU', {
                            timeZone: 'Australia/Perth',
                            timeZoneName: 'short',
                          })
                            .format(Date.now()).split(' ')[1]

enter image description here

Incensory answered 8/2 at 8:28 Comment(1)
Thanks, this was the pointer I needed.Hunsaker
L
0

in addition to above answer, to get the timezone name, pass tripple zzz it will return full timezone name

dayjs().tz("Europe/Paris").format("DD/MM/YYYY zzz"); // 14/12/2023 Central European Standard Time
Litta answered 14/12, 2023 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.