Compare 2 dates in format DD/MM/YYYY with javascript/jquery

Suppose I receive two dates from the datepicker plugin in format DD/MM/YYYY

var date1 = '25/02/1985';  /*february 25th*/
var date2 = '26/02/1985';  /*february 26th*/
/*this dates are results form datepicker*/

if(process(date2) > process(date1)){
   alert(date2 + 'is later than ' + date1);
}

What should this function look like?

function process(date){
   var date;
   // Do something
   return date;
}

Answers:

Answer

Split on the "/" and use the Date constructor.

function process(date){
   var parts = date.split("/");
   return new Date(parts[2], parts[1] - 1, parts[0]);
}
Answer

It could be more easier:

var date1 = '25/02/1985';  /*february 25th*/
var date2 = '26/02/1985';  /*february 26th*/

if ($.datepicker.parseDate('dd/mm/yy', date2) > $.datepicker.parseDate('dd/mm/yy', date1)) {

       alert(date2 + 'is later than ' + date1);

}

For more details check this out. Thanks.

Answer
function process(date){
   var parts = date.split("/");
   var date = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
   return date.getTime();
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.