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
Jquery Date.parse returning NaN in Chrome browser?
提问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
另见:日期