Jquery Date.parse returning NaN in Chrome browser?

前端 未结 2 1114
情话喂你
情话喂你 2020-12-03 21:07

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\';


        
相关标签:
2条回答
  • 2020-12-03 21:59

    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

    0 讨论(0)
  • 2020-12-03 21:59

    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 讨论(0)
提交回复
热议问题