Javascript Date Gives me EDT but I want EST

扶醉桌前 提交于 2019-12-10 23:37:29

问题


I am receiving a MySQL Timestamp in UTC and trying to covert it to the client's local timezone. However, When i do this I get the wrong time zone.

Ive formatted my DateTime String to be: var utcTime = 2014-05-15T13:00:00Z

However when after my conversion my dateObject is: Date {Thu May 15 2014 09:00:00 GMT-0400 (EDT)}. However, I want my Timezone to be GMT -0500 (EST).

I've searched online and saw there is a way to do this by appending "UTC" to a MYSQL formatted Timestamp.. However, this method does not work in all browsers.

If anyone has any insight on converting timezones i would appreciate it.


回答1:


The D in EDT stands for Daylight and the S in EST stands for Standard. EDT should be used during Summer in the U.S. and EST in the Winter (list of countries here). Is it possible that GMT -4 (EDT) is actually the right local time? If it would be more towards winter it would switch automatically to GMT -5 (EST). The client timezone together with daylight savings is handled automatically by Javascript.

For example, the default string representation of a certain date in Javascript should correctly choose between Standard time and Daylight Savings time based on the date object itself and the machine timezone:

var date = new Date(millisSinceUnixEpoch);
alert(date.toDateString() + ' ' + date.toTimeString());

Note: there's room for a lot of assumption though. E.g. not sure exactly how your 'conversion to local timezone' code looks like




回答2:


I've seen something similar to this. This MSDN article may explain it.

Handling daylight saving time using JavaScript

http://msdn.microsoft.com/en-us/library/ie/jj863688%28v=vs.85%29.aspx

In Windows Internet Explorer 9 and previous versions of Windows Internet Explorer, dates are customized by applying the ECMAScript specification's rules for storing daylight saving time adjusted times internally. To improve accuracy, especially with dates in the past (historical dates), Internet Explorer 10 relies on the system's rules for storing daylight saving time adjusted times. This topic contains the following sections:



来源:https://stackoverflow.com/questions/26453226/javascript-date-gives-me-edt-but-i-want-est

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!