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

后端 未结 4 1806
故里飘歌
故里飘歌 2020-12-18 20:57

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\"
         


        
4条回答
  •  情书的邮戳
    2020-12-18 21:28

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

    Date objects work with the number of milliseconds since The Epoch (Jan 1st 1970 at midnight GMT). They have methods like getDay and getMonth and such that use the local timezone of the JavaScript engine, and also functions like getUTCDay and getUTCMonth that use 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, but it was only added in ES5 and they got it wrong in the spec about what should happen if there's no timezone indicator on the string, so you need to be sure to always have a timezone indicator on it (for now, ES6 will fix it and eventually engines will reliably use ES6 behavior). Those strings look like this:

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

    You can produce that format using the toISOString method.

提交回复
热议问题