简体   繁体   English

默认javascript日期返回对象的正确格式是什么

[英]What's the correct format of the default javascript Date returned object

When the new Date() is called, it returns something like this当调用 new Date() 时,它会返回类似这样的内容

Wed Jul 13 2022 16:15:35 GMT-0600 (Central Standard Time)

So I was curious what is the correct format used in that case?所以我很好奇在这种情况下使用的正确格式是什么? I tried building it with the following format: E MMM d yyyy H:mm:ss zzzz However the result was我尝试使用以下格式构建它: E MMM d yyyy H:mm:ss zzzz但是结果是

Wed Jul 13 2022 16:15:00 GMT-06:00

This is the closest I can get.这是我能得到的最接近的。 I've been searching the internet but can't find the correct format.我一直在搜索互联网,但找不到正确的格式。 By the way, the local used is en_US顺便说一下,本地使用的是en_US

Example:例子:

let momentDate = moment(date, 'E MMM d yyyy H:mm:ss zzzz', true); 

If I execute the momentDate.isValid() it returns false.如果我执行 momentDate.isValid() 它返回 false。

From MDN : Date.prototype.toString() returns a string representation of the Date in the format specified in ECMA-262 .来自MDNDate.prototype.toString()ECMA-262中指定的格式返回 Date 的字符串表示形式。

That last part is described as:最后一部分描述为:

  • Optionally, a timezone name consisting of: (可选)时区名称,包括:
    • space空间
    • Left bracket, ie "("左括号,即“(”
    • An implementation dependent string representation of the timezone, which might be an abbreviation or full name (there is no standard for names or abbreviations of timezones), eg "Line Islands Time" or "LINT"时区的依赖于实现的字符串表示,可能是缩写或全名(时区的名称或缩写没有标准),例如“Line Islands Time”或“LINT”
    • Right bracket, ie ")"右括号,即“)”

Different date formatting libraries in different frameworks will have different ways to get a string representation of the time zone and, importantly, this is implementation-specific.不同框架中的不同日期格式库将有不同的方式来获取时区的字符串表示,重要的是,这是特定于实现的。 This SO answer shows one way to get that value, basically by outputting it from a date and parsing the result.这个 SO 答案显示了一种获取该值的方法,基本上是通过从日期输出它并解析结果。 But you should not rely on it (or really any aspect of date outputs, apart from toISOString() ) having any consistency between browsers or other execution environments.但是您不应该依赖它(或者实际上是日期输出的任何方面,除了toISOString() )在浏览器或其他执行环境之间具有任何一致性。

Until ECMAScript 2018 (edition 9), the format of the string returned by Date.prototype.toString was implementation dependent.在 ECMAScript 2018(第 9 版)之前,Date.prototype.toString 返回的字符串格式取决于实现。 Therefore it should not be relied upon to be in the specified format.因此,不应依赖于指定格式。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString#description https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString#description

output variations are by design, and allowed by the specification... you cannot expect to be able to compare the results of toLocaleString() to a static value输出变化是设计使然,规范允许...您不能期望能够将 toLocaleString() 的结果与静态值进行比较
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString#avoid_comparing_formatted_date_values_to_static_values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString#avoid_comparing_formatted_date_values_to_static_values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM