你如何在 Javascript 中检查日期是否是 UTC?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14522846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you check whether a date is UTC in Javascript?
提问by stefan.s
I've read this question: How do you convert a JavaScript date to UTC?
我读过这个问题: How do you convert a JavaScript date to UTC?
and based on this I implemented this conversion in a dateTools module as follows:
基于此,我在 dateTools 模块中实现了这种转换,如下所示:
[Update]
[更新]
var dt, utcTime;
dt = new Date();
utcTime = new Date(Date.UTC(dt.getFullYear(),
dt.getMonth(),
dt.getDate(),
dt.getHours(),
dt.getMinutes(),
dt.getSeconds(),
dt.getMilliseconds()));
Now I'd like to write unit tests. My idea was to check whether the result is actually in UTC, but I don't know how.
现在我想编写单元测试。我的想法是检查结果是否实际上是 UTC,但我不知道如何。
All the toString, toUTCString and similar methods seem to be identical for the input (non UTC) and output (UTC) date.
对于输入(非 UTC)和输出(UTC)日期,所有 toString、toUTCString 和类似方法似乎都相同。
Only the result of the getTime method differs.
只有 getTime 方法的结果不同。
Is there a way to check wheter a date is UTC in javascript? If not, is there a better idea to unit test this?
有没有办法在javascript中检查日期是否为UTC?如果没有,是否有更好的主意来对此进行单元测试?
To give more context:
提供更多上下文:
Only converting the it to a UTC string is not that helpful, because in the next step the date is sent to an Asp.net service and therefore converted to a string like:
仅将其转换为 UTC 字符串并没有多大帮助,因为在下一步中,日期将发送到 Asp.net 服务,因此转换为如下字符串:
'/Date([time])/'
with this code
用这个代码
var aspDate = '/Date(' + date.getTime() + ')/';
回答by Bergi
var aspDate = '/Date(' + date.getTime() + ')/';
Thisoutputs the internal UNIX epoch
value (UTC), which represents a timestamp. You can use the various toString
methods to get a more verbose string representation of that timestamp:
这将输出内部UNIXepoch
值 (UTC),它代表一个时间戳。您可以使用各种toString
方法来获得该时间戳的更详细的字符串表示:
.toString()
uses the users timezone, result is something like"Fri Jan 25 2013 15:20:14 GMT+0100"
(for me, at least, you might live in a different timezone).toUTCString()
uses UTC, and the result will look like"Fri, 25 Jan 2013 14:20:15 GMT"
.toISOString()
uses UTC, and formats the datestring according to ISO:"2013-01-25T14:20:20.061Z"
.toString()
使用用户时区,结果类似于"Fri Jan 25 2013 15:20:14 GMT+0100"
(至少对我来说,你可能生活在不同的时区).toUTCString()
使用UTC,结果看起来像"Fri, 25 Jan 2013 14:20:15 GMT"
.toISOString()
使用 UTC,并根据 ISO 格式化日期字符串:"2013-01-25T14:20:20.061Z"
So how do we construct the time value that we want?
那么我们如何构建我们想要的时间值呢?
new Date()
orDate.now()
result in the current datetime. No matter what the user's timezone is, the timestamp is just the current moment.new Date(year, month, …)
uses the users timezone for constructing a timestamp from the single values. If you expect this to be the same across your user community, you are screwed. Even when not using time values but only dates it can lead to odd off-by-one errors.- You can use the
setYear
,setMonth
, … andgetYear
,getMonth
… methods to set/get single values on existing dates according to the users timezone. This is appropriate for input from/output to the user. getTimezoneOffset()
allows you to query the timezone that will be used for all these
- You can use the
new Date(timestring)
andDate.parse
cannot be trusted. If you feed them a string without explicit timezone denotation, the UA can act random. And if you want to feed a string with a proper format, you will be able to find a browser that does not accept it (old IEs, especially).Date.UTC(year, month, …)
allows you to construct a timestamp from values in the UTC timezone. This comes in handy for input/output of UTC strings.- Every get/set method has a UTC equivalent which you can also use for these things.
new Date()
或Date.now()
导致当前日期时间。无论用户的时区是什么,时间戳只是当前时刻。new Date(year, month, …)
使用用户时区从单个值构建时间戳。如果您希望这在您的用户社区中是相同的,那么您就完蛋了。即使不使用时间值而只使用日期,它也会导致奇怪的一对一错误。- 您可以使用
setYear
,setMonth
, ... 和getYear
,getMonth
... 方法根据用户时区设置/获取现有日期的单个值。这适用于用户的输入/输出。 getTimezoneOffset()
允许您查询将用于所有这些的时区
- 您可以使用
new Date(timestring)
并且Date.parse
不能被信任。如果你给他们提供一个没有明确时区表示的字符串,UA 可以随机行动。如果您想以正确的格式提供字符串,您将能够找到不接受它的浏览器(尤其是旧的 IE)。Date.UTC(year, month, …)
允许您从 UTC 时区中的值构造时间戳。这对于 UTC 字符串的输入/输出非常方便。- 每个 get/set 方法都有一个 UTC 等效项,您也可以将其用于这些事情。
You can see now that your approach to get the user-timezone values and use them as if they were in UTC must be flawed. It means either dt
or utcTime
has the wrong value, although using the wrong output method may let it appear correct.
您现在可以看到,您获取用户时区值并像使用 UTC 一样使用它们的方法肯定有缺陷。这意味着要么dt
或utcTime
具有错误的值,尽管使用了错误的输出方法可以让它出现正确的。
回答by ic3b3rg
getTimezoneOffset
Syntax: object.getTimezoneOffset( ) This method returns the difference in minutes between local time and Greenwich Mean Time. This value is not a constant, as you might think, because of the practice of using Daylight Saving Time.
获取时区偏移量
语法:object.getTimezoneOffset() 此方法返回本地时间与格林威治标准时间之间的分钟差。由于使用夏令时的做法,您可能认为该值不是常数。
i.e.
IE
var myDate = new Date;
var myUTCDate = new Date(myDate - myDate.getTimezoneOffset() * 60000);
alert(myUTCDate);
note: 60000 is the number of milliseconds in a minute;
注意:60000 是一分钟的毫秒数;