I have two form fields with dates in the mm-dd-yyyy format. I need to validate that date2 > date1. How might I make this comparison?
Printable View
I have two form fields with dates in the mm-dd-yyyy format. I need to validate that date2 > date1. How might I make this comparison?
Code:if (new Date (date2) > new Date (date1)) {
...Do Something...
}
Code:d1="12-18-2009";
d2="12-15-2009";
date1=new Date(d1.split("-")[2], d1.split("-")[0], d1.split("-")[1]);
date2=new Date(d2.split("-")[2], d2.split("-")[0], d2.split("-")[1]);
if(date1<date2){do something}
Thanks for the feedback...here's what i finally did to get it to work...
I'm using jQuery, hence the $(..)... syntaxCode:var startDate = $('#start_date').val().replace('-','/');
var endDate = $('#end_date').val().replace('-','/');
if(startDate > endDate){
// do stuff here...
}
This does not use a JQuery solution, but consider this:
Advantage is that the argument passed to the cvrtDate function can be any of the following formatsCode:function cvrtDate(d) {
var t = d.split(/\D+/) // splits on any character(s) between digits
var dt = new Date();
dt.setFullYear(t[2],(t[0]-1),t[1]);
return dt;
}
'1/1/2010'
'1-1-2010'
'1.1.2010'
or even
'1/1-2010' or any other non-digit separator.