JavaScript help - compare two dates in the format (dd / MMM / yyyy)
I am trying to compare two values of a textbox in an aspx form. Both values represent dates. They are formatted as "dd / MMM / yyyy". How can I compare them in JavaScript to see if they are not equal and which one is greater, etc.?
Does JavaScript have a date constructor for strings like this? Any advice would be appreciated.
a source to share
Convert dates to UNIX timestamp then just compare values.
if (date1.getTime() > date2.getTime()) alert("bla");
To be more precise, the getTime function returns the number of milliseconds since 1970/01/01: http://www.w3schools.com/jsref/jsref_getTime.asp p>
And a little explanation about UNIX time: http://en.wikipedia.org/wiki/Unix_time
a source to share
Unfortunately, the method is Date.parse
not reliable, even for ISO format.
Below is a quick one-off function to parse the date of the very format you are currently using. If you like, you can add some sanity check to see if the day is within each month (don't forget leapyear then :)), but if you have reasonable control over the lines that are sent, it 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
a source to share
If you want to do a comparison, then you can simply do:
compareDates(d1, d2) {
d1 = d1.split("/").reverse().join("/");
d2 = d2.split("/").reverse().join("/");
return d1 == d2 ? 0 : d1 < d2 ? : -1 : 1;
}
Assuming the arguments are in the format "dd / mm / yyyy", this function will return 0 if d1 == d2, -1 if d1 <d2 and 1 if d1> d2.
PS: if your arrays don't know how reverse
, teach them how:
Array.prototype.reverse = function() {
var a = [];
for(var i=this.length; i; i--) a.push(this[i-1]);
return a;
}
EDIT: ok I didn't notice the "MMM" format. I guess I would use a hash of names per month.
function compareDates(date1,date2) {
var d1 = date1.split("/").reverse();
var d2 = date2.split("/").reverse();
var months = { 'Jan':'01', 'Feb':'02' }; // ...
d1[1] = months[d1[1]] || '00';
d2[1] = months[d2[1]] || '00';
date1 = d1.join("/");
date2 = d2.join("/");
return date1 == date2 ? 0 : date1 < date2 ? : -1 : 1;
}
a source to share