I have two dates 18-Aug-2010
and 19-Aug-2010
of this format. How to find whether which date is greater?
views:
84answers:
4
+2
A:
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
CMS
2010-08-18 07:15:01
+1
A:
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)) ...
Delan Azabani
2010-08-18 07:17:21
Instead of using hackish `* 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.
Denis 'Alpheus' Čahuk
2010-08-18 07:25:24
With the OP's format `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...
CMS
2010-08-18 07:27:04
@Denis in this case we don't even need to convert the strings to numbers, the [`Date` constructor](http://bclary.com/2004/11/07/#a-15.9.3) will convert each argument internally `ToNumber` ;)
CMS
2010-08-18 07:28:34
@CMS, I've fixed both problems. Fixed the first one by removing 'global search' tag. Fixed the second one by using `indexOf` to search.
Delan Azabani
2010-08-18 07:34:11
@Delan, nice, you just have to adjust your indexes again for the `date` and `year` matches (and you can remove those ugly `* 1` without problems) :)
CMS
2010-08-18 07:37:31
No, they are correct now when the `g` modifier is off because `0` is the entire matched string, while `1` and on are the captured matches. I'll remove the multiplication :D
Delan Azabani
2010-08-18 07:40:37
@Delan, I see, but then, the two `match` method calls (for the `date` and `year` variables) will give you exactly the same result. You will be able to capture only the `date` part, not the year.
CMS
2010-08-18 07:46:50
Aaaah, I see. I've fixed it to run one match call that captures all three 'fields' of the date. Thanks ;)
Delan Azabani
2010-08-18 07:54:01
+1
A:
The native Date
can parse "MMM+ dd yyyy", which gives:
function parseDMY(s){
return new Date(s.replace(/^(\d+)\W+(\w+)\W+/, '$2 $1 '));
}
+parseDMY('19-August-2010') == +new Date(2010, 7, 19) // true
parseDMY('18-Aug-2010') < parseDMY('19-Aug-2010') // true
matyr
2010-08-19 04:14:16