简体   繁体   English

在Javascript中将GMT中的日期/时间转换为EST

[英]Convert date/time in GMT to EST in Javascript

在Javascript中,无论用户设置如何,如何将GMT中的日期/时间转换为EST?

var tmpDate = New Date("enter any valid Date format here")

The javascript Date() function will automatically convert it to your local time. javascript Date()函数会自动将其转换为您当地的时间。

Example: 例:

var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012");
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012

Try some different values at jsfiddle: http://jsfiddle.net/R3huD/ 在jsfiddle尝试一些不同的值: http//jsfiddle.net/R3huD/

See here: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html 请参见: https//web.archive.org/web/1/http//articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

all you have to do is get the time in miliseconds and then add the offset in milliseconds and then shift back to a date time object 您所要做的就是以毫秒为单位获取时间,然后以毫秒为单位添加偏移量,然后再切换回日期时间对象

i was surprise to find the simplest solution. 我很惊讶找到最简单的解决方案。

If you have date in GMT, and when you create date in browser it always create in that time zone. 如果您在GMT中有日期,并且在浏览器中创建日期,则始终在该时区创建。

Simplest way is create date object with GMT itself and then do below 最简单的方法是使用GMT本身创建日期对象,然后在下面执行

starTime.setHours(starTime.getHours()+(starTime.getTimezoneOffset()/60));

That's it. 而已。 Even if you have date of future after day light saving like after November then also it will also work. 即使你有像11月之后一样的日复一日节约日期,那么它也会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM