JavaScript date difference calculation

Written by on May 2, 2010 in JavaScript - 9 Comments

Calculating date difference in JavaScript is almost as easy as in PHP, but requires a bit different approach. For this post i wrote simple “static” js object which can calculate difference between two dates in: days, weeks, months and years.

To each function you pass two arguments/dates, the first one should be lower then second, or you will get negative number on the output, however the number will still be valid, although with negative sign, so you just need to multiply it by “-1″.

Calculating date difference in days and weeks works pretty much the same. First we get difference between UNIX timestamps for first and second dates and then divide it by number of milliseconds in a day (for days calculation), or by number of milliseconds in a week (for weeks calculation).

Number of milliseconds in a month vary (because months have different number of days), so we are taking a different approach. For each date multiply year by 12 and add current month, that gives total number of months since year 0. Now substract both numbers and we are done.

Calculating difference between years i leave without a comment :) . Source code with example usage below.

var DateDiff = {
 
    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();
 
        return parseInt((t2-t1)/(24*3600*1000));
    },
 
    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();
 
        return parseInt((t2-t1)/(24*3600*1000*7));
    },
 
    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();
 
        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },
 
    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}
 
var dString = "May, 20, 1984";
 
var d1 = new Date(dString);
var d2 = new Date();
 
document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

9 Comments on "JavaScript date difference calculation"

  1. Fallout new vegas May 17, 2010 at 7:54 pm ·

    Great help thanks

  2. arefun September 3, 2010 at 10:15 am ·

    thanks bro.. it works..

  3. Srini November 3, 2010 at 4:23 am ·

    Nice simple and effective. Thanks for sharing!!

  4. Darryl Waterhouse January 10, 2011 at 2:25 pm ·

    Hiya,

    Try March, 27, 2011 to March, 28, 2011.
    It yields zero.
    :(

    I can not find a script anywhere that gets around this issue. And every Javascript engine (on every browser) fails to yield a value of one. :(

  5. Darryl Waterhouse January 10, 2011 at 2:40 pm ·

    It appears that there is a calculatory math problem in Javascript.
    Adding an arbitrary half when returning calculated values solves this problem.
    E.g.

    return parseInt((t2-t1)/(24*3600*1000) + .5);

    Note the addition of the “.5″, which overcomes the issue.

    Please feel free to correct/comment.
    Thanks,

    DW

  6. Jon Berenguer March 24, 2011 at 5:03 pm ·

    Great post, keep up the good work.

    Also thanks Darryl for the bug fix.

  7. daniel May 18, 2011 at 5:13 am ·

    It is 2011/05/18 Today.

    I change value of dString in source code as following
    var dString = “2011/04/30″

    This result in “Number of months since 2011/04/30: 1″

    I think the number of months is less than 1.

  8. john July 20, 2011 at 2:22 pm ·

    Thanks a lot, it saved my time.

  9. Jeff November 18, 2011 at 8:46 am ·

    Very clean. Adding hours, minutes and seconds was a breeze. Cheers.

Leave a Comment