Format date in a specific time zone

I use Moment.js to parse and format dates in my web application. As part of the JSON object, my internal server sends dates as the number of milliseconds from the UTC era (Unix offset).

Parsing dates in a specific time zone is simple - just add the RFC 822 time zone identifier to the end of the line before analysis:

// response varies according to your timezone moment('3/11/2012 13:00').utc().format("MM/DD HH:mm") // problem solved, always "03/11 17:00" moment('3/11/2012 13:00 -0400').utc().format("MM/DD HH:mm") 

But how do I format a date in a specific time zone ?

I need consistent results regardless of the current browser time, but I don’t want to display dates in UTC.

+141
javascript timezone momentjs
Mar 11 '13 at 20:05
source share
7 answers

As indicated in Manto's answer, .utcOffset() is the preferred method since 2.9.0. This function uses the real offset from UTC, and not the reverse offset (for example, -240 for New York during DST). A line offset of type "+0400" works the same as before:

 // always "2013-05-23 00:55" moment(1369266934311).utcOffset(60).format('YYYY-MM-DD HH:mm') moment(1369266934311).utcOffset('+0100').format('YYYY-MM-DD HH:mm') 

The older .zone() as a setter was deprecated in Moment.js 2.9.0. He accepted a string containing the time zone identifier (for example, β€œ-0400” or β€œ-04: 00” for 4 hours) or a number representing minutes behind UTC (for example, 240 for New York during DST).

 // always "2013-05-23 00:55" moment(1369266934311).zone(-60).format('YYYY-MM-DD HH:mm') moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm') 



To work with named time zones instead of numeric offsets, enable Moment Timezone and use .tz() instead:

 // determines the correct offset for America/Phoenix at the given moment // always "2013-05-22 16:55" moment(1369266934311).tz('America/Phoenix').format('YYYY-MM-DD HH:mm') 
+202
May 23 '13 at
source share

Use moment-time

 moment(date).tz('Europe/Berlin').format(format) 

Before you can access a specific time zone, you need to download it this way (or using the alternative methods described here )

 moment.tz.add('Europe/Berlin|CET CEST CEMT|-10 -20 -30') 
+40
Apr 18 '15 at 18:52
source share

Several answers already mention that the time-zone is the path to the named time zone. I just want to clarify something in this library, which bothers me quite a bit. There is a difference between these two statements:

 moment.tz(date, format, timezone) moment(date, format).tz(timezone) 



Assuming the time zone is not specified in the date passed in:

The first code takes a date and assumes that the time zone is the one that was transmitted. The second accepts the date, receives the time zone from the browser, and then changes the time and time zone in accordance with the transmitted time zone.

Example:

 moment.tz('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss', 'UTC').format() // "2018-07-17T19:00:00Z" moment('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss').tz('UTC').format() // "2018-07-18T00:00:00Z" 

My time zone is +5 from UTC. Thus, in the first case, it does not change and sets the date and time for the utc time zone.

In the second case, he assumes that the transferred date is at -5, then turns it into UTC, and that is why he spits out the date "2018-07-18T00: 00: 00Z"

NOTE. The format parameter is really important. If a missed moment can return to the Date class, this can lead to unpredictable behavior.




Assuming the time zone is in the date passed in:

In this case, they both behave the same.




Despite the fact that I now understand why this is so, I thought it was a rather confusing feature that is worth explaining.

+23
Jul 13 '18 at 22:03
source share

.zone () is deprecated and you should use utcOffset instead:

 // for a timezone that is +7 UTC hours moment(1369266934311).utcOffset(420).format('YYYY-MM-DD HH:mm') 
+16
Aug 19 '15 at 23:45
source share

I had the same issue with Moment.js. I set the time-zone, but the problem was not resolved. Then I did what I revealed here, set the time zone, and it works like a charm:

 moment(new Date({your_date})).zone("+08:00") 

Thank you so much!

+4
Sep 17 '14 at 16:18
source share

It just came, and since I had the same problem, I just posted the results I came up with

with parsing, you can update the offset (that is, I am analyzing the data (1.1.2014), and I only need the date, January 1, 2014. At GMT + 1, I get December 31, 2013. first.

 moment(moment.utc('1.1.2014').format()); 

Well, it came in handy for me to support time zones

AT

+3
May 27 '15 at 10:26
source share

You can try this,

Here you can get the date based on the time zone of the client (browser).

 moment(new Date().getTime()).zone(new Date().toString().match(/([-\+][0-9]+)\s/)[1]).format('YYYY-MM-DD HH:mm:ss') 

The regular expression basically returns the offset value.

Hurrah !!

-four
Aug 21 '18 at 9:52
source share



All Articles