J.J.Baxter - My place on the web - interests, hobbies, thoughts & ruminations
Home / Blog
Books
Links
Images
Aug Sep-2010 Oct
SMTWTFS
2930311234
567891011
12131415161718
19202122232425
262728293012
3 4 5 6 7 8 9
Reset Calendar
Snorg Girls
Programming Tips
Sherlock Holmes
Mythbusters
Gallipoli
Nova Science Now
Ajax Loading Images
The Girls of Dr. Who
Keratosis Pilaris (KP)
Amazing Water
Buy me a drink? I'll have a Cappuccino, thanks.
Random Images
Random Link
Newcastle TV Guide #1
Random Book
Night Work
by Laurie R King






All Rights Reserved 2008
JJBaxter.com

J.J.Baxters Development Tips & Snippets
backBack
 Compare two dd-MMM-yyyy dates (JavaScript : 08-Apr-2008)
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;
}