Alan2
Alan2

Reputation: 24572

Which time zone does JavaScript `new Date()` use?

I have a C# application that return in JSON an expiration date of an authentication token like this:

"expirationDate":"Fri, 27 Mar 2015 09:12:45 GMT"

In my TypeScript I check that the date is still valid here:

isAuthenticationExpired = (expirationDate: string): boolean => {
    var now = new Date().valueOf();
    var exp: any = Date.parse(expirationDate).valueOf();
    return exp - now <= 0;
};

What I would like to know is what time zone does new Date() use when it is returning a date?

Upvotes: 31

Views: 62723

Answers (4)

T.J. Crowder
T.J. Crowder

Reputation: 1074385

What time zone does the Javascript new Date() use?

Date objects store the number of milliseconds since The Epoch (Jan 1st 1970 at midnight GMT). They have methods like getDay and getMonth and such that provide values in the local timezone, and also functions like getUTCDay and getUTCMonth that provide values in UTC (loosely, GMT).

If you're parsing a string, you need to be sure that the string is in a format that Date knows how to parse. The only defined format in the specification is a simplified derivative of ISO-8601: YYYY-MM-DDTHH:mm:ss.sssZ. But that was only added in ES5 (and then updated in ES2015 and ES2016). The time zone of the string is determined by whether it has a UTC offset indicator (Z, -08:00, +05:30, etc.) and whether it's date-only or date/time. Here are some examples:

function test(str) {
    const dt = new Date(str);
    const p = document.createElement("pre");
    p.textContent =
        `String: "${str}"\n` +
        `UTC:    ${dt.toISOString()}\n` +
        `Local:  ${dt.toLocaleString()}`
    ;
    document.body.appendChild(p);
}

// No UTC offset provided, date-only form => parsed as UTC:
test("2015-08-09");

// No UTC offset provided, date/time form => parsed as local time:
test("2015-08-09T09:32:54");

// Has UTC offset "Z" (for "no offset") => parsed as UTC:
test("2015-08-09T09:32:54.427Z");

// Has UTC offset -08:00 => parsed as UTC minus eight hours:
test("2015-08-09T09:32:54.427-08:00");


I don't recommend relying on it, but even though it's not specified, all major JavaScript engines will successfully parse the American-specific formats MM/DD/YYYY, MM/DD/YYYY hh:mm, MM/DD/YYYY hh:mm:ss, or MM/DD/YYYY hh:mm:ss.SSS (although the milliseconds portion is ignored by some). Note the order in the date portion: month, day, year. Having a UTC offset indicator is not broadly supported and will cause an error on most engines, so don't include one. Without one, all of those are parsed as local time (even the date-only one). Note that the date field separator must be / (08/09/2015), not - (08-09-2015).

function test(str) {
    const dt = new Date(str);
    const p = document.createElement("pre");
    p.textContent =
        `String: "${str}"\n` +
        `UTC:    ${dt.toISOString()}\n` +
        `Local:  ${dt.toLocaleString()}`
    ;
    document.body.appendChild(p);
}

test("08/09/2015");
test("08/09/2015 09:32");
test("08/09/2015 09:32:54");
test("08/09/2015 09:32:54.427");

But again: I don't recommend relying on that.

Upvotes: 4

Grokify
Grokify

Reputation: 16334

JavaScript will use the client's local time but it also has UTC / GMT methods. The following is from Mozilla:

The JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.

While methods are available to access date and time in both UTC and the localtime zone, the date and time are stored in the local time zone:

Note: It's important to keep in mind that the date and time is stored in the local time zone, and that the basic methods to fetch the date and time or its components all work in the local time zone as well.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 23

Armīns Nikolajevs
Armīns Nikolajevs

Reputation: 107

As other said, JavaScript uses clients system time, but you can create date object from server time, so every client will get the same current time.

var date = new Date("<?php echo date("Y-m-d H:i:s");?>");

It will work only on page load. If you want to check later for dates still valid, then you need to synchronize current date with server date every couple of seconds.

Upvotes: 2

Valentin Montmirail
Valentin Montmirail

Reputation: 2640

By default, JS will use your browser timezone, but if you want to change the display, you can for example use the toString() function ;)

var d1=new Date();
d1.toString('yyyy-MM-dd');       //returns "2015-03-27" in IE, but not FF or Chrome
d1.toString('dddd, MMMM ,yyyy')  //returns "Friday, March 27,2015" in IE, but not FF or Chrome

Upvotes: 2

Related Questions