I have problem showing timezone with moment.js.
I tried with this code:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A Z");
and I get return, for example: 08/05/2015 06:18 PM +02:00
, which is fine, but I want that my output be like 08/05/2015 06:18 PM WEDT
or something like that, with abbreviations of timezones.
Tried using this code, but I get empty timezone on the end:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A z");
or
var result = moment(someDate).format("MM/DD/YYYY HH:mm A zz");
UPDATE
So as @Matt Johnson suggested, I used this approach to show time zone using moment-timezone-with-data.js and tzdetect.js:
var tzName = tzdetect.matches()[0];
var result = moment.tz(myDate, tzName).format("MM/DD/YYYY h:mm A zz");
I have problem showing timezone with moment.js.
I tried with this code:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A Z");
and I get return, for example: 08/05/2015 06:18 PM +02:00
, which is fine, but I want that my output be like 08/05/2015 06:18 PM WEDT
or something like that, with abbreviations of timezones.
Tried using this code, but I get empty timezone on the end:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A z");
or
var result = moment(someDate).format("MM/DD/YYYY HH:mm A zz");
UPDATE
So as @Matt Johnson suggested, I used this approach to show time zone using moment-timezone-with-data.js and tzdetect.js:
var tzName = tzdetect.matches()[0];
var result = moment.tz(myDate, tzName).format("MM/DD/YYYY h:mm A zz");
Share
Improve this question
edited Aug 5, 2015 at 16:54
freshbm
asked Aug 5, 2015 at 15:30
freshbmfreshbm
5,6226 gold badges50 silver badges80 bronze badges
2 Answers
Reset to default 12As described in the documentation:
Note: as of 1.6.0, the z/zz format tokens have been deprecated. Read more about it here.
The general problem is that time zone abbreviations are not available from the browser through a consistent API. In order to provide them, one has to have an external source of data.
You may want to look into using the moment-timezone addon. It provides time zone information, including abbreviations. You would have to know the specific time zone you are working with. For example:
moment.tz("2015-08-05T00:00:00+01:00", "Europe/London").format("MM/DD/YYYY hh:mm A z");
// "08/05/2015 12:00 AM BST"
Also, you shouldn't mix HH
(hours of the 24-hour clock) with A
(the 12-hour am/pm designator). Either use hh
with A
, or use HH
without A
.
To Convert UTC datetime to user's current timezone the solution is to use moment-timezone instead of moment.(We have all dates as UTC in DB). Other timezones shouldn't be an issue either.
const timeZoneString = Intl.DateTimeFormat().resolvedOptions().timeZone
const getFormattedDateTimeWithTZ = (date) => {
return moment((date)).tz(timeZoneString).format('ddd, MMM DD YYYY, h:mm A zz')
}
// outputs Tue, Mar 08 2022, 4:00 PM PKT
getFormattedDateTimeWithTZ('2022-03-08T03:00:00.000-08:00')