Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

momentjs

dayjs

Trying to migrate from Moment.js to Dayjs but only thing I can't get working is 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 plugin 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 web I see a lot of folks saying solution is format('', {timeZone}) but 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 example of 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

like image 739
Labithiotis Avatar asked Sep 20 '20 08:09

Labithiotis


People also ask

How do you show timezone in moment?

var result = moment(someDate). format("MM/DD/YYYY HH:mm A Z");

Does moment js use local timezone?

The main moment. js library has full functionality for working with UTC and the local time zone.

What is Dayjs?

Day. js is a minimalist JavaScript date library for parsing, validating, manipulating, and formatting dates. It is an alternative library to Moment. js and has largely compatible API.

Should I use moment js?

Moment. js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.


1 Answers

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

Updating to the newest version and setup the plugins properly, and it should work. Example below

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

dayjs.extend(timezone)
dayjs.extend(utc)
dayjs.extend(advanced)
dayjs().tz('Europe/Paris').format('DD/MM/YYYY z')
like image 87
mrl Avatar answered Oct 17 '22 22:10

mrl