Skip to content
Advertisement

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)

JavaScript

Advertisement

Answer

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()))

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement