What time zone does the new Date () use in JavaScript?

I have a C # application that returns the expiration date of an authentication token in JSON as follows:

"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 would I like to know how to use the time zone of new Date() when it returns a date?

+11
javascript date typescript
source share
4 answers

JavaScript will use the local time of the client, but also has UTC / GMT methods. Following from Mozilla:

The JavaScript Date object supports a number of generic (UTC) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time set by the world time standard. Local time is the time known to the computer running JavaScript.

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

Note: It is important to remember that the date and time are stored in the local time zone, and that the basic methods for obtaining date and time or their components also work in the local time zone.

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

+9
source share

Like all of the above, JavaScript uses the system time of clients, but you can create a date object from server time, so each client will receive the same current time.

 var date = new Date("<?php echo date("Ymd H:i:s");?>"); 

It will only work when the page loads. If you want to check later if the dates are still valid, you need to synchronize the current date with the server date every couple of seconds.

+2
source share

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

 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 
+1
source share

What time zone is the new Date () Javascript used in?

Date objects work with the number of milliseconds since the Epoch (January 1, 1970 at midnight GMT). They have methods like getDay and getMonth and those that use the local time zone for the JavaScript engine, as well as functions like getUTCDay and getUTCMonth that use UTC (free, GMT).

If you parse a string, you must be sure that the string is in a format that Date can parse. Only a specific simplified derivative format from ISO-8601 is specified in the specification, but it was added only in ES5 and they were mistaken in the specification of what should happen if there is no time zone indicator in the line, so you definitely need to have a time zone indicator (on ES6 will fix it for now, and ultimately the engines will reliably use ES6 behavior). These lines are as follows:

 "2015-03-27T09:32:54.427Z" 

You can create this format using the toISOString method.

+1
source share

All Articles