[英]How do you check whether a date is UTC in Javascript?
I've read this question: How do you convert a JavaScript date to UTC? 我已经读过这个问题: 你如何将JavaScript日期转换为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? 有没有办法检查javascript中的日期是UTC? If not, is there a better idea to unit test this?
如果没有,是否有更好的想法进行单元测试?
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() + ')/';
var aspDate = '/Date(' + date.getTime() + ')/';
This outputs the internal UNIX epoch
value (UTC) , which represents a timestamp. 这将输出内部UNIX
epoch
值(UTC) ,表示时间戳。 You can use the various toString
methods to get a more verbose string representation of that timestamp: 您可以使用各种
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) .toString()
使用用户时区,结果类似于"Fri Jan 25 2013 15:20:14 GMT+0100"
(对我而言,至少,你可能生活在不同的时区) .toUTCString()
uses UTC, and the result will look like "Fri, 25 Jan 2013 14:20:15 GMT"
.toUTCString()
使用UTC,结果看起来像"Fri, 25 Jan 2013 14:20:15 GMT"
.toISOString()
uses UTC, and formats the datestring according to ISO: "2013-01-25T14:20:20.061Z"
.toISOString()
使用UTC,并根据ISO格式化日期字符串: "2013-01-25T14:20:20.061Z"
So how do we construct the time value that we want? 那么我们如何构建我们想要的时间价值呢?
new Date()
or Date.now()
result in the current datetime. new Date()
或Date.now()
导致当前日期时间。 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. new Date(year, month, …)
使用用户时区从单个值构造时间戳。 If you expect this to be the same across your user community, you are screwed. setYear
, setMonth
, … and getYear
, getMonth
… methods to set/get single values on existing dates according to the users timezone. setYear
, setMonth
,...和getYear
, getMonth
...方法根据用户时区在现有日期设置/获取单个值。 This is appropriate for input from/output to the user. getTimezoneOffset()
allows you to query the timezone that will be used for all these getTimezoneOffset()
允许您查询将用于所有这些的时区 new Date(timestring)
and Date.parse
cannot be trusted. new Date(timestring)
和Date.parse
不可信任。 If you feed them a string without explicit timezone denotation, the UA can act random. Date.UTC(year, month, …)
allows you to construct a timestamp from values in the UTC timezone. Date.UTC(year, month, …)
允许您根据UTC时区中的值构造时间戳。 This comes in handy for input/output of UTC strings. 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. 您现在可以看到,获取用户时区值并将其用作UTC时的方法必须存在缺陷。 It means either
dt
or utcTime
has the wrong value, although using the wrong output method may let it appear correct. 这意味着
dt
或utcTime
具有错误的值,尽管使用错误的输出方法可能会使其显示正确。
getTimezoneOffset 使用getTimezoneOffset
Syntax: object.getTimezoneOffset( ) This method returns the difference in minutes between local time and Greenwich Mean Time. 语法:object.getTimezoneOffset()此方法返回本地时间与格林威治标准时间之间的差异。 This value is not a constant, as you might think, because of the practice of using Daylight Saving Time.
由于使用夏令时的做法,您可能认为此值不是常量。
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是一分钟内的毫秒数;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.