Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number of days into years, months, days

I have two date pickers that calculates the number of days there are between the two dates. At the moment I'm outputting the number of days (see code below) which is kind of meaningless. I want to output that number in years, months, days. How can I do that?

E.g So 01/01/14 to 01/02/15 = 397 days which then becomes 1 year(s), 1 month(s), 1 day(s)

var diff = endDate - startDate;
dayCount = diff / ( 60 * 60 * 24 * 1000 ); // secs * mins * hours * milliseconds
dayCount = Math.round( dayCount ) + this.options.countAdjust;
return dayCount;
like image 844
Rob Avatar asked Oct 02 '14 10:10

Rob


3 Answers

You have a bug in your calculation : it's 0 month. And if you mean d/m/y then 1 year, 1 month, and 0 day old.

you said between the two dates ( not include) - look here

Anyway here is the right code which include actually count each month - how many days it has ! ( leap year consideration):

notice :

I instantiated it as d/m/yyy. feel free to send right pattern in :

alert(getAge( new Date(1978,11,22),new Date()))

function getAge(date_1, date_2)
{
  
//convert to UTC
var date2_UTC = new Date(Date.UTC(date_2.getUTCFullYear(), date_2.getUTCMonth(), date_2.getUTCDate()));
var date1_UTC = new Date(Date.UTC(date_1.getUTCFullYear(), date_1.getUTCMonth(), date_1.getUTCDate()));


var yAppendix, mAppendix, dAppendix;


//--------------------------------------------------------------
var days = date2_UTC.getDate() - date1_UTC.getDate();
if (days < 0)
{

    date2_UTC.setMonth(date2_UTC.getMonth() - 1);
    days += DaysInMonth(date2_UTC);
}
//--------------------------------------------------------------
var months = date2_UTC.getMonth() - date1_UTC.getMonth();
if (months < 0)
{
    date2_UTC.setFullYear(date2_UTC.getFullYear() - 1);
    months += 12;
}
//--------------------------------------------------------------
var years = date2_UTC.getFullYear() - date1_UTC.getFullYear();




if (years > 1) yAppendix = " years";
else yAppendix = " year";
if (months > 1) mAppendix = " months";
else mAppendix = " month";
if (days > 1) dAppendix = " days";
else dAppendix = " day";


return years + yAppendix + ", " + months + mAppendix + ", and " + days + dAppendix + " old.";
}


function DaysInMonth(date2_UTC)
{
var monthStart = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth(), 1);
var monthEnd = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth() + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
return monthLength;
}


alert(getAge(new Date(1978, 11, 22), new Date()))
like image 125
Royi Namir Avatar answered Oct 11 '22 20:10

Royi Namir


You can use link shown below , it has more detailed explanation. JSFIDDLE The detailed code is -

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 d1 = new Date("01/01/14");
var d2 = new Date("01/02/15");
var months= DateDiff.inYears(d1, d2)*12 ;
var month = DateDiff.inMonths(d1, d2) - months;
var days = DateDiff.inYears(d1, d2)*365;
var dy = DateDiff.inDays(d1, d2) - days;
alert(DateDiff.inYears(d1, d2) + " Year " + month + " Month "+ dy + " Days");

Link

like image 41
Arindam Nayak Avatar answered Oct 11 '22 21:10

Arindam Nayak


Considering
1 Year => 365 Days
1 MONTH => 30 Days
1 WEEK => 7 Days
value => NUMBER OF DAYS

function parseDays (value) 
        { 
           var year, months, week, days;
         
           year = value >= 365 ? Math.floor(value / 365) : 0;
           value = year ? value - (year*365) : value;
        
           months = value >= 30 ? Math.floor((value % 365) / 30) : 0;
           value = months ? value - (months*30) : value;
        
           week = value >= 7 ? Math.floor((value % 365) / 7) : 0;
           value = week ? value - (week*7) : value;
        
           days = value < 7 ? Math.floor((value % 365) % 7) : 0;
        
           console.log("years = ", year); 
           console.log("months = ",months); 
           console.log("weeks = ",week); 
           console.log("days = ", days);     
        }
like image 1
Matul Jain Avatar answered Oct 11 '22 21:10

Matul Jain