Search code examples
javascriptdatetimedateprototypejs

javascript - convert date to long based on the language specific format


I have two date pickers one for startDate and another one is for endDate.

The date pickers is returning me values in following formats, based on the language of the application :

language selected by user    datepicker value(start and end)
========================     ===============================
          en                     03/30/2012 | 04/12/2012
          es                     30/03/2012 | 12/04/2012

My javascript code is something like :

var startDate = new Date ($('startDate'+counter).value);
var endDate = new Date($('endDate'+counter).value);

if(endDate.getTime() > startDate.getTime()){
//if part logic
}
else{
// else part logic
}

above code is working fine with en language, but failing with es, because of the different date format in the datepicker.

That means endDate.getTime() and startDate.getTime() is returning wrong value in case of es language.

I am using prototypejs...


Solution

  • Try to use Date.parse() in your code:

    var startDate = Date.parse($('startDate'+counter).value);