最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery Date.parse returning NaN in Chrome browser? - Stack Overflow

programmeradmin4浏览0评论

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

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

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

Can anybody know a workaround for this?

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

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

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

Can anybody know a workaround for this?

Share Improve this question edited Nov 1, 2011 at 10:13 Febin J S asked Nov 1, 2011 at 10:06 Febin J SFebin J S 1,3683 gold badges26 silver badges55 bronze badges 8
  • 2 Unable to reproduce, works for me: jsfiddle.net/b77DE/1 – Darin Dimitrov Commented Nov 1, 2011 at 10:09
  • Oh, and by the way the parse method expects the string to be formatted as RFC822 or ISO 8601 date. – Darin Dimitrov Commented Nov 1, 2011 at 10:13
  • 4 It's culture related. I get NaN when I alert enddate – Emre Erkan Commented Nov 1, 2011 at 10:14
  • 3 Date.parse (lowercase!). And Date format must be 'mm/dd/yyyy' for Date.parse! – Andrew D. Commented Nov 1, 2011 at 10:42
  • 1 @kara:- As you said that this is culture specific issue then how come this works in other browsers except chrome? – Febin J S Commented Nov 1, 2011 at 11:08
 |  Show 3 more comments

2 Answers 2

Reset to default 12

If you want to parse a date without local differences, use the following, instead of 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

According to this

dateString A string representing an RFC822 or ISO 8601 date.

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

发布评论

评论列表(0)

  1. 暂无评论