I have two dates 18-Aug-2010 and 19-Aug-2010 of this format. How to find whether which date is greater?
-
Your question is a possible duplicate of this question: stackoverflow.com/questions/492994/… The solution described there should help you.Manoj Govindan– Manoj Govindan2010-08-18 07:05:16 +00:00Commented Aug 18, 2010 at 7:05
-
is the language for the text guaranteed to be english?davidsleeps– davidsleeps2010-08-19 04:41:21 +00:00Commented Aug 19, 2010 at 4:41
-
@David can't get what you are trying to say?ACP– ACP2010-08-19 04:44:10 +00:00Commented Aug 19, 2010 at 4:44
-
18-Aug is English specific...10-Oct is English, but in Indonesian (for example) it would be 10-Okt etc...the MMM desc is language dependent...unless you are guaranteeing a specific languagedavidsleeps– davidsleeps2010-08-19 05:12:33 +00:00Commented Aug 19, 2010 at 5:12
5 Answers
You will need to create a custom parsing function to handle the format you want, and get date objects to compare, for example:
function customParse(str) {
var months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'],
n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;
while(n--) { months[months[n]]=n; } // map month names to their index :)
matches = str.match(re); // extract date parts from string
return new Date(matches[3], months[matches[2]], matches[1]);
}
customParse("18-Aug-2010");
// "Wed Aug 18 2010 00:00:00"
customParse("19-Aug-2010") > customParse("18-Aug-2010");
// true
Comments
You can do the parsing manually, for your given format, but I'd suggest you use the date.js library to parse the dates to Date objects and then compare. Check it out, its awesome!
And moreover, its a great addition to your js utility toolbox.
Comments
Firstly, the 'dd-MMM-yyyy' format isn't an accepted input format of the Date constructor (it returns an "invalid date" object) so we need to parse this ourselves. Let's write a function to return a Date object from a string in this format.
function parseMyDate(s) {
var m = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
var match = s.match(/(\d+)-([^.]+)-(\d+)/);
var date = match[1];
var monthText = match[2];
var year = match[3];
var month = m.indexOf(monthText.toLowerCase());
return new Date(year, month, date);
}
Date objects implicitly typecast to a number (milliseconds since 1970; epoch time) so you can compare using normal comparison operators:
if (parseMyDate(date1) > parseMyDate(date2)) ...
8 Comments
* 1 hacks to cast the result to an integer, you should rather use parseInt(s.match(/(\d+)/g)[1], 10) to get the Integer object.s.match(/(\d+)/g); will return you a two element array, e.g. "12-Aug-2010".match(/(\d+)/g); // ["12", "2010"], you are accessing the 1 and 2 indexes. Also you need some way to map the three letter month name with the month number...Date constructor will convert each argument internally ToNumber ;)indexOf to search.date and year matches (and you can remove those ugly * 1 without problems) :)Update: IE10, FX30 (and likely more) will understand "18 Aug 2010" without the dashes - Chrome handles either
so Date.parse("18-Aug-2010".replace("/-/g," ")) works in these browsers (and more)
Hence
function compareDates(str1,str2) {
var d1 = Date.parse(str1.replace("/-/g," ")),
d2 = Date.parse(str2.replace("/-/g," "));
return d1<d2;
}