Sadly, the Date.parse
way isn't reliable, not even for ISO format.
Here below is a quick one-off-ish function for parsing dates of the very format you currently use. If you like you can add some sanity checking to see if the day is within each month's bounds (don't forget about leapyear then :)), but if one has reasonable control over the strings one sends in, this works.
function parseThisYourVeryKnownFormatToDate(dateString /* '12/Jun/2010' */) {
function getMonthIdx(monthName) {
var months = {
'Jan':0, 'Feb':1, 'Mar':2, 'Apr':3, 'May':4, 'Jun':5
, 'Jul':6, 'Aug':7, 'Sep':8, 'Oct':9, 'Nov':10, 'Dec':11
};
return months[monthName];
}
var format = /^(\d+)\/(\w{3})\/(\d{4})$/;
var match = format.exec(dateString);
if (!match) {return undefined;}
var day = match[1], monthIdx = getMonthIdx(match[2]), year = match[3];
return new Date(year, monthIdx, day);
}
var testDates = ['10/Jan/2008', '15/Jun/1971', '31/Dec/1999', 'bogus/date/foo'];
for (var idx=0, len=testDates.length; idx<len; ++idx) {
console.log(parseThisYourVeryKnownFormatToDate(testDates[idx])); // real date objects, except for the last
}
var d0 = (parseThisYourVeryKnownFormatToDate('15/Apr/2009'));
var d1 = (parseThisYourVeryKnownFormatToDate('12/Apr/2009'));
console.log(d0+' is after '+d1+': '+(d0.getTime()>d1.getTime())); // says true
console.log(d1+' is after '+d0+': '+(d1.getTime()>d0.getTime())); // says false
console.log(d0+' is after '+d0+': '+(d0.getTime()>d0.getTime())); // says false