如何将UTC日期/时间转换为EST日期/时间 [英] How to convert UTC date/time to EST date/time

查看:470
本文介绍了如何将UTC日期/时间转换为EST日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码以正确的格式获取当前UTC时间,但客户端已回来并要求时间戳现在是EST而不是UTC。我搜索过Google和stackoverflow,但无法找到适合我现有代码的答案。

I'm using the following code to get the current UTC time in the correct format, but the client has come back and asked that the timestamp now be in EST instead of UTC. I have searched Google and stackoverflow, but can't find an answer that works with my existing code.

var currentdate = new Date(); 
var datetime = currentdate.getFullYear() + ":"
            + ("0" + (currentdate.getUTCMonth()+1)).slice(-2) + ":"
            + ("0" + currentdate.getUTCDate()).slice(-2) + ":"
            + ("0" + currentdate.getUTCHours()).slice(-2) + ":"  
            + ("0" + currentdate.getUTCMinutes()).slice(-2) + ":" 
            + ("0" + currentdate.getUTCSeconds()).slice(-2);

我们要做的是为EST设置一致的时间戳,无论浏览器位于何处世界,因此最初使用UTC时间。

What we are trying to do is set a consistent timestamp to EST regardless of where the browser is located in the world, hence the use of the UTC time originally.

谢谢!

推荐答案

一些事情...


  • 术语EST可用于北部的东部标准时间美国,或澳大利亚东部标准时间。大多数时区缩写都不是唯一的,因此您应该更具体。

  • The term "EST" can be used for either Eastern Standard Time in North America, or Eastern Standard Time in Australia. Most time zone abbreviations are not unique, so you should be more specific.

EST也没有完整定义时区。它仅定义了冬季使用的时区的一部分。另一半叫做Eastern Daylight Time,缩写为EDT。您的客户可能意味着东部时间,需要考虑两个

EST also does not define a time zone in its entirety. It only defines part of the time zone that is used during the winter months. The other half is called Eastern Daylight Time, abbreviated EDT. Your client probably meant "Eastern Time", which would need to take both into account.

定义时区的典型方法是来自 IANA时区数据库的标识符。您可以在 timezone标记维基中阅读更多相关信息。例如,如果您的客户在美国意味着东部时间,那么您的时区就是America / New_York。

The typical way to define time zones is as an identifier from the IANA time zone database. You can read more about it in the timezone tag wiki. For example, if your client meant Eastern Time in the United States, then your time zone is "America/New_York".

JavaScript本身并不知道任何事情关于时区,除了它自己和UTC。 (bjb568提供的链接无法正确处理夏令时。)为了在客户端上使用它们,您需要使用我在此列出的其中一个库

JavaScript inherently doesn't know anything about time zones, other than it's own and UTC. (The link that bjb568 gave does not handle daylight saving time properly.) In order to work with them on the client, you will need to use one of the libraries I list here.

您的当前代码在输出方面有点奇怪。通常冒号仅用于分隔时间,并且您将它们用于所有部分。无论如何,它没有转换任何东西,它只是输出UTC。

Your current code is a bit strange in terms of output. Usually colons are used for separating only the time, and you are using them for all parts. Anyway, it's not converting anything, it's just outputting UTC.

你可能会对像 moment.js ,它是 moment-timezone 附加组件。例如:

You might do well with a library like moment.js and it's moment-timezone add-on. For example:

moment().tz("America/New_York").format("YYYY-MM-DD HH:mm:ss")


这篇关于如何将UTC日期/时间转换为EST日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆