Patric
Patric

Reputation: 2999

Calculating timespan in years, months and days in JavaScript

I want to calculate the timespan between two dates (note: input format is dd.MM.yyyy, see code below). Special thing is, that don't want to use 30 days for every month and 360 days for a year. I rather want the difference in "human format" (don't know how to call it).

Let's say, I want to calculate difference (including the last day) from October 1 (2014) until March 17, 2015. From October to February this would make 5 months. And then the rest would be 18 days (from day 1 to 17, including 17th day). So the result should be :

0 years, 5 months, 18 days

This sort of calculation of course ignores that some months have 31, 30, 28 or 29 days (except for the day calculation, when dates are in such a form: start date: October 17th, 2014; end date: Januar 12th, 2015).

Unfortunately I didn't found any JS lib that already implements this sort of calculation. Moments.js, doesn't seem to have any methods for this.

So I started creating my own code but I'm still always off some days (2-3 days) from the expected result. And to be honest, if I look at my code I have the feeling that it sucks and there must be a more intelligent and elegant way to calculate this (in my example I'm outputting it to a span):

function getTimeDifferenceContract(startDateStr, endDateStr) {
    var returnObject = { "years": null, "months": null, "days": null };
    var startDateArray = startDateStr.split('.');
    var endDateArray = endDateStr.split('.');
    var startMonthIx = startDateArray[1]-1;
    var endMonthIx = endDateArray[1]-1;
    var startDate = new Date(startDateArray[2], startMonthIx, startDateArray[0]);
    var endDate = new Date(endDateArray[2], endMonthIx, endDateArray[0]);

    var endDateFixed = new Date(endDate.getTime()+(1*24*60*60*1000));

    if (endDate > startDate) {
        var years = 0;
        var months = 0;
        var days = 0;
        var dateDiff = endDateFixed.getTime() - startDate.getTime();

        var sD = startDate.getDate();
        var sM = startDate.getMonth()+1;
        var sY = startDate.getFullYear();

        var eD = endDateFixed.getDate();
        var eM = endDateFixed.getMonth()+1;
        var eY = endDateFixed.getFullYear();

        if (sY == eY && sM == eM) {         
            days = Math.floor(dateDiff / (1000 * 60 * 60 * 24));
        }
        else if (sY == eY) {
            if (sD > eD) {
                months = eM - sM - 1;
                var startMonthRestDays = getMonthdays(sM, sY) - sD;
                days = startMonthRestDays + eD;
            }
            else {
                months = eM - sM;
                days = eD - sD;
            }
        }
        else {
            years = eY - sY - 1;
            var monthForYears = 0;
            if (years > 0) {
                monthForYears = (years - 1) * 12;
            } else {
                monthForYears = years * 12;
            }
            months = (12 - sM) + (eM - 1) + (monthForYears)
            var startMonthRestDays = getMonthdays(sM, sY) - sD;
            days = startMonthRestDays + eD - 0;
        }

        var lastMonth = eM - 1;
        var yearForEndMonthDays = eY;
        if (lastMonth < 1) {
            lastMonth = 12;
            yearForEndMonthDays = eY - 1;
        }       
        var endMonthDays = getMonthdays(lastMonth, yearForEndMonthDays);
        if (days >= endMonthDays) {          
            months = months + 1;
            days = days - endMonthDays - 1;
        }
        if (months >= 12) {
            years = years + 1;
            months = months - 12;
        }

        returnObject.years = years;
        returnObject.months = months;
        returnObject.days = days;
    }
    return returnObject;
}

function main() {   
    var difference = getTimeDifferenceContract("30.09.2014", "01.10.2015");
    var years = difference.years;
    var months = difference.months;
    var days = difference.days;

    jQuery('#myText').text(years + " years, " + months + " months, " + days + " days");
}

main();

Fiddle: http://jsfiddle.net/4ddL27gx/2/

Any ideas to solve this?

Upvotes: 2

Views: 3226

Answers (2)

xploshioOn
xploshioOn

Reputation: 4125

I hope this will help:

Obtain difference between two dates in years, months, days in JavaScript

i was having the same necessity but no answer so i wrote a functions that seems to work...

Upvotes: 2

mfarouk
mfarouk

Reputation: 654

i have used moment to achieve the result you need

var moment1 = new moment(new Date("30/Sep/2014"));
var moment2 = new moment(new Date("01/Oct/2015"));

var diffInMilliSeconds = moment2.diff(moment1);

var duration = moment.duration(diffInMilliSeconds);

var years = duration.years();
var months = duration.months();
var days = duration.days();

$('.div1').text(moment1.format());
$('.div2').text(moment2.format());
$('.div3').text(years + " years, " + months + " months, " + days + " days");

jsfiddle http://jsfiddle.net/mfarouk/qLqm3uuh/1/

Upvotes: 1

Related Questions