I needed to compare two dates to ensure users entered dates that were different. The
Date.parse javascript function enables dates to be compared but it requires dates to be in
dd-mmm, yyyy format! So I wrote this function that converts dd-mmm-yyyy to dd-mmm, yyyy.
function FormatDateForParse(sDate) {
// for Date.parse the format must be 'mmm-dd, yyyy'
// if dates are 10 chars long then add a leading 0
if (sDate.length==10) sDate = "0" + sDate;
// convert to 'mmm-dd, yyyy'
return sDate.substring(3,7) + sDate.substring(0,2) + ", " + sDate.substring(7,11);
}
Then in my webpage I used it like this
var dStart = FormatDateForParse(oForm.rplstart.value); // convert dates to 'mmm-dd, yyyy'
var dEnd = FormatDateForParse(oForm.rplend.value); // so we can use the Date.parse function
if (Date.parse(dStart) >= Date.parse(dEnd)) {
alert("Start date must be earlier than end date");
return false;
}