JavaScript new Date() 使用什么时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29297257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:07:26  来源:igfitidea点击:

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

javascriptdatetypescript

提问by Alan2

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

我有一个 C# 应用程序,它以 JSON 形式返回身份验证令牌的到期日期,如下所示:

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

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

在我的 TypeScript 中,我检查日期在这里是否仍然有效:

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?

我想知道的new Date()是返回日期时使用的时区是什么?

采纳答案by Grokify

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

JavaScript 将使用客户端的本地时间,但它也有 UTC / GMT 方法。以下内容来自 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.

JavaScript Date 对象支持许多 UTC(通用)方法以及本地时间方法。UTC,也称为格林威治标准时间 (GMT),是指世界时间标准设定的时间。本地时间是执行 JavaScript 的计算机已知的时间。

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:

虽然可以使用方法访问 UTC 和本地时区中的日期和时间,但日期和时间存储在本地时区中:

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

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

回答by Armīns Nikolajevs

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.

正如其他人所说,JavaScript 使用客户端系统时间,但您可以从服务器时间创建日期对象,因此每个客户端将获得相同的当前时间。

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.

它仅适用于页面加载。如果您想稍后检查日期是否仍然有效,那么您需要每隔几秒钟将当前日期与服务器日期同步。

回答by Valentin Montmirail

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

默认情况下,JS 将使用您的浏览器时区,但如果您想更改显示,您可以使用该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

回答by T.J. Crowder

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

Javascript new Date() 使用什么时区?

Dateobjects work with the number of milliseconds since The Epoch (Jan 1st 1970 at midnight GMT). They have methods like getDayand getMonthand such that use the local timezone of the JavaScript engine, and also functions like getUTCDayand getUTCMonththat use UTC (loosely, GMT).

Date对象使用自大纪元(格林威治标准时间 1970 年 1 月 1 日午夜)以来的毫秒数。它们有像getDaygetMonth这样的方法,它们使用 JavaScript 引擎的本地时区,也有像getUTCDaygetUTCMonth那样使用 UTC(松散地,GMT)的函数。

If you're parsing a string, you need to be sure that the string is in a format that Dateknows how to parse. The only definedformat 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:

如果您正在解析一个字符串,您需要确保该字符串的格式Date知道如何解析。规范中唯一定义的格式是ISO-8601 的简化派生形式,但它仅在 ES5 中添加,并且他们在规范中弄错了关于如果字符串上没有时区指示符会发生什么情况的错误,因此您需要确定总是有一个时区指示器(目前,ES6 将修复它,最终引擎将可靠地使用 ES6 行为)。这些字符串看起来像这样:

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

You can produce that format using the toISOStringmethod.

您可以使用该toISOString方法生成该格式。