How to make JavaScript Date.prototype.toString () always display local timezone

I am setting UTC + 0 time and want it to be displayed on different computers in the local timezone of each computer. But when I run the code in Chrome on my computer (VMware VM), it gave me the following information:

Date.UTC(2014, 0, 27, 6) // 1390802400000
var now = new Date()               // Tue Aug 05 2014 07:32:38 GMT-0400 (EDT)
now.getTime()     // 1407238358829
new Date(1407238358829)  // Tue Aug 05 2014 07:32:38 GMT-0400 (EDT)
new Date(1390802400000)  // Mon Jan 27 2014 01:00:00 GMT-0500 (EST)

      

My computer is at GMT-4, but why does the last line show EST instead of EDT?

I am also trying to use the same code on other computers such as an EC2 instance (GMT + 0) and a friend of mine (GMT + 8), these computers will only display the time of their local timezone.

But why does my computer display the time in a different time zone?

+3


source to share


3 answers


The code is working correctly. This is how time zones work. Hopefully some correct terminology will clear up the confusion for you.

A "time zone offset" is not the same as "time zone". For more information, see the "Time Zone! = Offset" section of the wiki .

You said:

My computer is in GMT-4 mode

This is not true. Your computer is in the North American time zone "Eastern Time". On Linux / Mac, this will probably display as "America/New_York"

. In windows, it will appear in the control panel as Eastern Time (US & Canada).

This time zone has a time offset of UTC-05: 00 in the winter months and UTC-04: 00 in the summer months when daylight saving time is in effect. We call this Eastern Standard Time (or EST) and Eastern Daylight Time (or EDT) respectively. Although they are sometimes referred to as "time zones", you can think of them as "time zone segments" - since they only refer to a portion of the actual time zone.

Repeat:



EST = Eastern Standard Time = UTC-05:00 = GMT-5
EDT = Eastern Daylight Time = UTC-04:00 = GMT-4

ET = Eastern Time = "America/New_York" = "Eastern Time (US & Canada)"
  - Uses EST in the winter
  - Uses EDT in the summer

      

new Date(1390802400000)

shows as EST because it is the value in January, which is in winter.

You also said:

I am also trying to use the same code on other computers such as an EC2 instance (GMT + 0) and a friend of mine (GMT + 8), these computers will only display the time of their local timezone.

The EC2 instance is most likely configured for UTC. This works best for servers. This means that it will always have a zero offset. This will not change for daylight saving time.

Your partner computer is most likely in a time zone that does not follow daylight saving time. For example (and I am assuming here) it may be in the time zone "Asia/Shanghai"

that is used in China. Since China does not use daylight saving time, its offset is always UTC + 08: 00.

Here you can see a list of time zones and their offsets.

+6


source


Dates in Javascript are stored as UTC time, more precisely, as milliseconds from a specific date (more precisely, 1/1/1970 00:00:00). You can print them in local time with date.toLocaleString()

. There are also special time-only and date-only methods: date.toLocaleDateString()

and date.toLocaleTimeString()

. They return a string because local time is for display, not storage.

Generally: ALWAYS store and treat dates as UTC time until you show them to users. If you are reading the local time from the user, you need to copy them to UTC as soon as possible and keep them that way until you show them again.




In your case, the reason you are getting 1 date in EDT and 1 in EST is due to daylight saving time (setting the clock 1 hour forward or backward). For about six months, parts of the US East Coast (such as New York) are in a different time zone than the other half of the year. In your case, August 5 and January 27 work effectively in different time zones for those using DST. In fact, you cannot have a January time in EDT, because that time zone only exists somewhere in mid-March and mid-October.

Source: http://www.timeanddate.com/library/abbreviations/timezones/na/edt.html

+4


source


When you do:

new Date(1390802400000)  // Mon Jan 27 2014 01:00:00 GMT-0500 (EST)

      

A new Date object is created with a time value from 1390802400000. If you just write this to the output (alert, console, whatever) then the default is the same as Date.toString, which returns a string in a convenient format based on the local timezone offset.

If you want to see Date as UTC, you can use the toISOString method :

var d = new Date(1390802400000);
console.log(d.toISOString());   // 2014-01-27T06:00:00.000Z

      

Or you can use UTC methods (like getUTCHours ) and construct the string in whatever format you like.

My computer is at GMT-4, but why is the last line showing EST instead of EDT

Because on that day, your computer assumes the local time zone is EST (presumably US Eastern Standard Time, not Australian Eastern Standard Time). It?

+2


source







All Articles