2

I have two date strings in DDMMYYYY format. say startdate="18/02/2013" and enddate ="26/02/2013".

How can I compare these dates. I want enddate to be greater than or equal to startdate
Thanks for Your Time.

SRJ
  • 198
  • 1
  • 4
  • 22
  • http://stackoverflow.com/questions/2587345/javascript-date-parse – Satpal Oct 23 '13 at 08:50
  • 4
    Use [`moment.js`](http://momentjs.com) and be happy. – user2864740 Oct 23 '13 at 08:50
  • Checkout http://stackoverflow.com/questions/13264457/comparing-times-using-moment-js – drtf Oct 23 '13 at 08:50
  • Also check [date](http://www.datejs.com/) library. – raduns Oct 23 '13 at 08:55
  • 3
    I'll suggest you to stay away from libraries for simple tasks like this. – Gurpreet Singh Oct 23 '13 at 08:56
  • There's absolutely no need for a library to do something as easy as this. – Qantas 94 Heavy Oct 23 '13 at 09:42
  • @GurpreetSingh While it's interesting to do this by hand once, I'll use moment.js for this any day (just as I use jQuery.xhr over XMLHttpRequest anyday). I get to pick my tools. Go tools! The reason for using a better refined API becomes much more "clear" when considering that it's very easy to *visualize* what is occurring: `moment.parse(theString, "DD/MM/YYYY")` (also notice that this made it easy to identify the error with the stated MMDDYYY format). – user2864740 Oct 24 '13 at 05:27
  • Thanks all. @wared I meant DDMMYYYY – SRJ Oct 24 '13 at 05:32
  • 2
    I have added a demo. Please don't pay attention to the "-1", my answer has been hit by the mad downvoter :) –  Oct 24 '13 at 06:05

3 Answers3

3

I'm a fan of moment.js and consider it a core part of my toolkit whenever I have to deal with dates and times - especially when any form of parsing or formatting is involved.

You're free to do the parsing by hand and invoke the appropriate Date constructor manually, but consider the following which I consider simple and intuitive.

var startDate = moment.parse("18/02/2013", "DD/MM/YYYY");
var endDate = moment.parse("26/02/2013", "DD/MM/YYYY");

if (endDate.isAfter(startDate)) {
   // was after ..
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
2

Does this solution suits your needs (demo : http://jsfiddle.net/wared/MdA3B/)?

var startdate = '18/02/2013';
var d1 = startdate.split('/');
d1 = new Date(d1.pop(), d1.pop() - 1, d1.pop());

var enddate = '26/02/2013';
var d2 = enddate.split('/');
d2 = new Date(d2.pop(), d2.pop() - 1, d2.pop());

if (d2 >= d1) {
    // do something
}

Keep in mind that months begin with 0. MDN doc :

month : Integer value representing the month, beginning with 0 for January to 11 for December.

  • General code suggestions: 1) avoid resuing the same variable name for different purposes; 2) *avoid* side-effects in function call arguments (indexing would likely be be apt here); 3) use small functions liberally - http://jsfiddle.net/9Qeye/2/ – user2864740 Oct 24 '13 at 19:25
  • @user2864740 Well, after the shameless cheater and the mad downvoter, here comes the bombastic teacher :D Hey, it's a joke, I'm just tired to debate over obviousnesses. Thanks for your precious advices. –  Oct 24 '13 at 21:25
0
var d1 = Date.parse("18/02/2013");
var d2 = Date.parse("26/02/2013");
if (d1 > d2) {
  alert ("do something");
}
LHH
  • 3,233
  • 1
  • 20
  • 27
  • 1
    `Date.parse("18/02/2013")` results in NaN here. I'm not exactly sure what format(s) `Date.parse` *should* parse and I avoid it because I don't know what the specification says on the matter or what browser/localization quirks exists. (Locally, it appears to parse `Date.parse("02/18/2013")` correctly. Go [United Sates dates](http://en.wikipedia.org/wiki/Date_format_by_country)!) – user2864740 Oct 24 '13 at 05:46