关于 JavaScript new Date() 和 Date.parse()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4321270/
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 11:53:25  来源:igfitidea点击:

Regarding JavaScript new Date() and Date.parse()

javascriptdate

提问by Chitresh

This is my code :

这是我的代码:

  var exampleDate='23-12-2010 23:12:00';  
  var date=new Date(exampleDate);//returns invalid Date
  var date1=Date.parse(exampleDate);//returns NAN

I want to convert above string into date. So I have written a code as you have seen above.

我想将上面的字符串转换为日期。所以我写了一个你在上面看到的代码。

This code is running fine in IEand Opera, but datevariable is returning me an invalid Date and date1is returning NANin mozilla Firefox, what should I do?

这段代码在IEand 中运行良好Opera,但date变量返回一个无效的 Date 并date1返回NANin mozilla Firefox,我该怎么办?

采纳答案by Romani

Above format is only supported in IE and Chrome.

以上格式仅在 IE 和 Chrome 中支持。

so try with another formats. following are some formats and there supporting browsers.

所以尝试使用其他格式。以下是一些格式和支持浏览器。

<script type="text/javascript">

//var dateString = "03/20/2008";  // mm/dd/yyyy [IE, FF]

 var dateString = "2008/03/20";  // yyyy/mm/dd [IE, FF]
// var dateString = "03-20-2008";  // mm-dd-yyyy [IE, Chrome]
// var dateString = "March 20, 2008";  // mmmm dd, yyyy [IE, FF]
// var dateString = "Mar 20, 2008";  // mmm dd, yyyy [IE, FF]

// Initalize the Date object by passing the date string variable
var myDate = new Date(dateString);
alert(myDate); 
</script>

回答by casablanca

The string in your example is not in any of the standard formats recognized by browsers. The ECMAScript specification requires browsers to be able to parse only one standard format:

您示例中的字符串不是浏览器可识别的任何标准格式。ECMAScript 规范要求浏览器只能解析一种标准格式:

The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

This format includes date-only forms:

YYYY

YYYY-MM

YYYY-MM-DD

It also includes time-only forms with an optional time zone offset appended:

THH:mm

THH:mm:ss

THH:mm:ss.sss

Also included are “date-times” which may be any combination of the above.

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

格式如下: YYYY-MM-DDTHH:mm:ss.sssZ

此格式包括仅日期格式:

YYYY

YYYY-MM

YYYY-MM-DD

它还包括带有可选时区偏移量的仅时间形式:

THH:mm

THH:mm:ss

THH:mm:ss.sss

还包括“日期时间”,它可以是上述的任意组合。

如果字符串不符合该格式,该函数可能会回退到任何特定于实现的启发式或特定于实现的日期格式。无法识别的字符串或包含格式为 String 的非法元素值的日期将导致 Date.parse 返回 NaN。

So in your example, using 2010-12-23T23:12:00is the only string guaranteed to work. In practice, most browsers also allow dates of the format DD Month YYYYor Month DD, YYYY, so strings like 23 Dec 2010and Dec 23, 2010could also work.

所以在你的例子中, using2010-12-23T23:12:00是唯一保证有效的字符串。在实践中,大多数浏览器也允许格式为日期DD Month YYYYMonth DD, YYYY,所以像23 Dec 2010和这样的字符串Dec 23, 2010也可以工作。

回答by maerics

You could parse it manually with a regular expression then call the date constructor with the date elements, as such:

您可以使用正则表达式手动解析它,然后使用日期元素调用日期构造函数,如下所示:

var parseDate = function(s) {
  var re = /^(\d\d)-(\d\d)-(\d{4}) (\d\d):(\d\d):(\d\d)$/;
  var m = re.exec(s);
  return m ? new Date(m[3], m[2]-1, m[1], m[4], m[5], m[6]) : null;
};
var dateStr = '23-12-2010 23:12:00';
parseDate(dateStr).toString(); //=> Thu Dec 23 2010 23:12:00 GMT-0800

回答by hade

JavaScript should support conversion at least from the following dateStrings:

JavaScript 至少应该支持从以下日期字符串转换:

* yyyy/MM/dd
* MM/dd/yyyy
* MMMM dd, yyyy
* MMM dd, yyyy

Try with:

尝试:

  var exampleDate='12/23/2010 23:12:00';
  var date = new Date(exampleDate); 

回答by darioo

Use datejsand this code:

使用datejs和此代码:

var exampleDate='23-12-2010 23:12:00';
var myDate = Date.parseExact(exampleDate, 'dd-MM-yyyy hh:mm:ss');

myDateshould be a correctly constructed Dateobject.

myDate应该是一个正确构造的Date对象。

回答by Pearl

Just use in this format:

只需以这种格式使用:

var exampleDate='2010-12-23 23:12:00';