0

I have been trying to format the date (OffsetDateTime) received from backend and convert the same in local time zone using moment.

I have imported the library.

import moment from "moment-timezone/builds/moment-timezone-with-data";

To format the date, I have used -

moment("2020-04-21T06:02:06.059934-05:00").format(
        "YYYY-MM-DD HH:mm:ss z"
      )

The output received is - 2020-04-21 06:02:06. The timezone is not displayed.

However if I use -

moment("2020-04-21T06:02:06.059934-05:00").format(
        "YYYY-MM-DD HH:mm:ss ZZ"
      )

The output received is - 2020-04-21 06:02:06 -0500.

How do I display the timezone in CST or IST or any local browser timezone?

Arnab
  • 195
  • 2
  • 14
  • This is covered in [the documentation for Moment Timezone](https://momentjs.com/timezone/docs/#/using-timezones/formatting/)... you have to use `moment.tz(...).format(...)`. – Heretic Monkey Apr 24 '20 at 15:16
  • While trying in the below format, I got an error as - moment .tz("2020-04-21T06:02:06.059934-05:00") .format("YYYY-MM-DD HH:mm:ss z") index.js:1472 Moment Timezone has no data for 2020-04-21T06:02:06.059934-05:00. Also the current time in UTC has been returned. 2020-04-24 15:48:42 UTC – Arnab Apr 24 '20 at 15:52
  • Did you happen to read the linked documentation at all? You have to give it a time zone name. The offset is not enough, because multiple time zones match that offset. – Heretic Monkey Apr 24 '20 at 15:55
  • @HereticMonkey I would like to get the browser time zone instead of specifying the time zone. – Arnab Apr 25 '20 at 16:02
  • `moment("2020-04-21T06:02:06.059934-05:00").tz(moment.tz.guess()).format("YYYY-MM-DD HH:mm:ss z")` – Heretic Monkey Apr 25 '20 at 22:59

0 Answers0