I have a date in the format like 2019-05-18T19:30:00-0400 which I need to display as 05/18/2019 07:30 PM EST using momentjs or plain javascript. I tried several ways, for example, moment(new Date('2019-05-18T19:30:00-0400')).format('MM-DD-YYYY h:mm A zz')
Asked
Active
Viewed 276 times
0
-
I tried this, moment('2019-05-18T19:30:00-0700').format('DD/MM/YYYY h:mm A zz') which is displaying it like 18/05/2019 7:30 PM, but no timezone value – user1614862 May 22 '19 at 20:28
-
If you want to use plain JavaScript, have a look at [`toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString). If you want to use moment, have a look at [moment-timezone](http://momentjs.com/timezone/), you could have something like: `moment.tz('2019-05-18T19:30:00-0400', 'America/New_York').format('MM/DD/YYYY hh:mm A zz')`. See also [this question](https://stackoverflow.com/q/40494995/4131048). – VincenzoC May 22 '19 at 22:54
-
I do not know timezone (America/New_York) details when I get date string. It can be any date with any offset 2019-05-18T19:30:00+0100, if I use toLocaleString(), it is converting it to browser's timezone, not to the timezone that represents in date string. – user1614862 May 23 '19 at 00:31
1 Answers
0
It is impossible to determine a time zone from an offset alone. There are many offsets that belong to more than one time zone. Thus rendering an abbreviation is impossible.
See Time Zone != Offset in the timezone tag wiki for further details, and refer to the list of tz database time zones if you would like examples.
However, from your comments it appears you that you would also like to keep the same local time and offset as you are given. Moment can help with that part, using its (inappropriately named) parseZone
function:
moment.parseZone("2019-05-18T19:30:00+0100").format("MM/DD/YYYY hh:mm A ZZ")
//=> "05/18/2019 07:30 PM +0100"

Matt Johnson-Pint
- 230,703
- 74
- 448
- 575