javascript Jquery Date.parse 在 Chrome 浏览器中返回 NaN?

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

Jquery Date.parse returning NaN in Chrome browser?

javascriptjquerygoogle-chrome

提问by Febin J S

I have a senario where i have to parse two dates for example start date and end date.

我有一个 senario,我必须解析两个日期,例如开始日期和结束日期。

var startdate = '02/01/2011';
var enddate = '31/12/2011';

But if we alert start date

但是如果我们提醒开始日期

 alert(Date.Parse(startdate)); i will get 1296498600000

but if i alert enddate

但如果我提醒结束日期

 alert(Date.Parse(enddate)); i will get NaN

But this is working in other browsers except Chrome, But in other browsers

但这在除 Chrome 之外的其他浏览器中有效,但在其他浏览器中

alert(Date.Parse(enddate)); i will get 1370889000000

Can anybody know a workaround for this?

任何人都可以知道解决方法吗?

采纳答案by Rob W

If you want to parse a date without local differences, use the following, instead of Date.parse():

如果要解析没有本地差异的日期,请使用以下内容,而不是Date.parse()

var enddate = '31/12/2011'; //DD/MM/YYYY
var split = enddate.split('/');
// Month is zero-indexed so subtract one from the month inside the constructor
var date = new Date(split[2], split[1] - 1, split[0]); //Y M D 
var timestamp = date.getTime();

See also: Date

另见:日期

回答by Connell

According to this

根据这个

dateString A string representing an RFC822 or ISO 8601 date.

dateString 表示 RFC822 或 ISO 8601 日期的字符串。

I've tried your code and I also get NaNfor the end date, but if i swap the date and month around, it works fine.

我试过你的代码,我也得到NaN了结束日期,但如果我交换日期和月份,它工作正常。