Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Date.parse returning NaN in Chrome browser?

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?

like image 252
Febin J S Avatar asked Nov 01 '11 10:11

Febin J S


2 Answers

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

like image 137
Rob W Avatar answered Nov 20 '22 05:11

Rob W


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.

like image 25
Connell Avatar answered Nov 20 '22 07:11

Connell