最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - How do you check whether a date is UTC in Javascript? - Stack Overflow

programmeradmin3浏览0评论

I've read this question: How do you convert a JavaScript date to UTC?

and based on this I implemented this conversion in a dateTools module as follows:

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

All the toString, toUTCString and similar methods seem to be identical for the input (non UTC) and output (UTC) date.

Only the result of the getTime method differs.

Is there a way to check wheter a date is UTC in javascript? 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 service and therefore converted to a string like:

'/Date([time])/'

with this code

var aspDate = '/Date(' + date.getTime() + ')/';

I've read this question: How do you convert a JavaScript date to UTC?

and based on this I implemented this conversion in a dateTools module as follows:

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

All the toString, toUTCString and similar methods seem to be identical for the input (non UTC) and output (UTC) date.

Only the result of the getTime method differs.

Is there a way to check wheter a date is UTC in javascript? 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 service and therefore converted to a string like:

'/Date([time])/'

with this code

var aspDate = '/Date(' + date.getTime() + ')/';
Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Jan 25, 2013 at 13:34 stefan.sstefan.s 3,4972 gold badges31 silver badges45 bronze badges 7
  • 3 @VirendraRajput Uh... why did you link to this question? – Dave Newton Commented Jan 25, 2013 at 13:38
  • @DaveNewton Bot, which is trying to get a "Fanatic" badge for its "master". – Engineer Commented Jan 25, 2013 at 13:39
  • Can't you just take a known time not in UTC, run it through your converter, and check to see if it es back as the correct time in UTC? – Dave Newton Commented Jan 25, 2013 at 13:39
  • 1 I think it's UTC by default, isn't it? – bfavaretto Commented Jan 25, 2013 at 13:44
  • @Engineer isn't fanatic badge earned when you visit the site 100 days consecutively? – keune Commented Jan 25, 2013 at 13:45
 |  Show 2 more ments

2 Answers 2

Reset to default 11
var aspDate = '/Date(' + date.getTime() + ')/';

This outputs 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:

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

So how do we construct the time value that we want?

  • new Date() or Date.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 munity, 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. This is appropriate for input from/output to the user.
    • getTimezoneOffset() allows you to query the timezone that will be used for all these
  • new Date(timestring) and Date.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 es in handy for input/output of UTC strings.
    • Every get/set method has a UTC equivalent which you can also use for these things.

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.

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

i.e.

var myDate = new Date;
var myUTCDate = new Date(myDate - myDate.getTimezoneOffset() * 60000);
alert(myUTCDate);

note: 60000 is the number of milliseconds in a minute;

发布评论

评论列表(0)

  1. 暂无评论