简体   繁体   English

如何在Javascript中检查日期是否为UTC?

[英]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. 如果您希望在您的用户社区中使用它,那么您就会被搞砸了。 Even when not using time values but only dates it can lead to odd off-by-one errors . 即使不使用时间值但只使用日期,它也可能导致奇怪的逐个错误
    • You can use the setYear , setMonth , … and getYear , getMonth … methods to set/get single values on existing dates according to the users timezone. 您可以使用setYearsetMonth ,...和getYeargetMonth ...方法根据用户时区在现有日期设置/获取单个值。 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. 如果您为它们提供没有明确时区表示的字符串,则UA可以随机操作。 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). 如果你想用适当的格式提供一个字符串,你将能够找到一个不接受它的浏览器(特别是旧的IE)。
  • 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. 这对于UTC字符串的输入/输出非常方便。
    • Every get/set method has a UTC equivalent which you can also use for these things. 每个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. 您现在可以看到,获取用户时区值并将其用作UTC时的方法必须存在缺陷。 It means either dt or utcTime has the wrong value, although using the wrong output method may let it appear correct. 这意味着dtutcTime具有错误的值,尽管使用错误的输出方法可能会使其显示正确。

  • 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.

相关问题 如何将毫秒转换为 Javascript UTC 日期? - How do you convert Milliseconds into a Javascript UTC date? 如何检查PHP和/或Javascript中的CSV文件是否已损坏? - How do you check whether a CSV file is corrupted in PHP and/or Javascript? 在 JavaScript 中如何轻松检查变量是否为数字? - How do you easily check whether a Variable is number or not in JavaScript? 如果您应该在 Javascript 中将 UTC 时间修改为 -1 或 +1,如何根据时区进行检查? - How do you check according to a timezone if you should modify the UTC time to -1 or +1 in Javascript? 在调用getVarDate之前是否需要将JavaScript日期转换为UTC? - Do you need to convert a JavaScript Date to UTC before calling getVarDate? 如何在 UTC 中为当前时间创建一个新的 javascript 日期 object? - How do you create a new javascript date object for the current time in UTC? 检查UTC日期已更改JavaScript - Check utc Date is changed JavaScript 给定日期/时间和时区,如何将其转换为UTC时间戳? - Given a date/time and timezone, how do you convert that into a UTC timestamp? 如何将 Javascript 时间戳转换为 UTC 格式? - How do you convert a Javascript timestamp into UTC format? 如何在Javascript中构建UTC日期作为对象,而不是数字? - How do I construct a UTC date in Javascript as an object, not a number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM