Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert days in years js

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()))
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery onclick click 
Javascript :: requirejs example 
Javascript :: catch errors async await javascript 
Javascript :: combine the values of 2 arrays in key = value jquery 
Javascript :: Javascript format date / time 
Javascript :: Get width of screen on resize event 
Javascript :: ios/main.jsbundle no such file or directory react native 
Javascript :: for loop in javascript 
Javascript :: How to fetch data from an api async and await 
Javascript :: uppercase first letter js 
Javascript :: running a function in a function javascript 
Javascript :: js copy text 
Javascript :: map keys to list node js 
Javascript :: how to return the max and min of an array in javascript 
Javascript :: convert datetime to timestamp javascript 
Javascript :: add background image react native 
Javascript :: popper.js and jquery 
Javascript :: pass data from child component to parent component 
Javascript :: js max array 
Javascript :: javascript remove last item 
Javascript :: javascript telegram bot 
Javascript :: angular get class list for element 
Javascript :: json data types 
Javascript :: Detect the city on application launch via geolocation react native 
Javascript :: how to build jquery post data 
Javascript :: change version webpack-dev-middleware 
Javascript :: redux saga fetch api 
Javascript :: puppeteer js headless mode 
Javascript :: how to use the foreach method in javascript 
Javascript :: button disappears after click javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =