Skip to content

JavaScript compare two dates

23 August 2011

It is often necessary to compare dates used in variety of web forms. There are hundreds of discussions on the topic as well as many solutions supplied, but as this functionality is not yet build natively in JavaScript I’m going to add one more suggestion.

The problem: Are 23.8.2011 and 8/23/2011 equals as dates? Actually the problem is that usually dates are entered as an input string in forms or are composed part by part to a single string.

Additionally date/time settings on the clients machine may differ from server and though we easily perceive ‘23.8.2011’ as a date, JavaScript will return different answer depending on the settings and the string we are testing.

So we could use JavaScript Date’s prototype property to add a method that will compare a date object with a date represented as a string:

Date.prototype.test = function (dateStr){
// check in parameter for null and split it
  var dt = (dateStr || "").split(/[\.\/\-]/g);
// date needs at least date, month and year
  if(dt.length<3){  throw new Error(-1, "Invalid date ["+d+"]");  }
// create date using dd mm yyyy template
  var da = new Date(parseInt(dt[2], 10), parseInt(dt[1], 10) -1, parseInt(dt[0], 10));
// compare values of the objects
  return this.valueOf()===da.valueOf();

Now we could test with:

var t = new Date();

Result will show whether date supplied by the string parameter is same as current date or not.

Short though this sample gives the idea how we could compare two dates. One of the problems in the function test is that it does not take in consideration neither date delimiter nor order of date, month and year parts.

To solve this we need a format template as a second parameter.

Date.prototype.isEqual =  function (d, f){
     // missing format template
     if((f||'').length == 0)  {  throw new Error(-1, "Invalid date format ["+f+"].");  }
     // date string does not match format string
     if((d||'').length != (f||'').length)  {  throw new Error(-1, "Date format ["+f+"] does not match supplied date ["+d+"].");  }
     var dt = (d || "").split(/[\.\/\-]/g);  // accept any well known date delimiter
     if(dt.length==0){  throw new Error(-1, "Invalid date ["+d+"]");  }
     var fo = f.split(/\s/);
 // this is date part
     f = fo[0].split(/[\.\/\-]/g);
// if hour part is supplied add it
     if((fo[1]||'').length > 0)  { 
       f = f.concat(f, fo[1].split(/[:\.]/)); 
     var da = new Date();
     for(var i=0, len=f.length; i<len; i++){
// each date part must match the position in supplied format string
       switch (f[i]) {
         case 'd':
         case 'dd':
           da.setDate(parseInt(dt[i], 10))
         case 'm':
         case 'mm':
           da.setMonth(parseInt(dt[i], 10)-1)
         case 'yy':
           da.setYear(parseInt(dt[i], 10))
         case 'yyyy':
           da.setFullYear(parseInt(dt[i], 10))
// return comparison result.
     return this.valueOf()===da.valueOf();

Now we could test it

var t = new Date();
t.isEqual('23.08.2011', ''); // will retrun true/false depending on parameters.

That’s it. I hope to be useful for somebody.


From → JavaScript

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: