There is a code that considers age:

var today = new Date(); var yyyy = today.getFullYear(); var Name = question2[1].FIO + '(' + (yyyy - question2[1].Age)+')'; 

But here's the problem, you need to finish writing, years, year, etc.

How to implement it?

    2 answers 2

    We look at the penultimate digit: if 1 is years, otherwise according to the last digit: 1 - year, 2-4 years, the rest - years.

     var years = [ 1822,1922,1948,1990,1995,1996,2008,2014,2016 ] years.forEach(function(year){ let thisYear = new Date().getFullYear(); let yearsOld = thisYear - year; document.write(yearsOld + " " + getYearAddition(yearsOld) + "<br>"); }); function getYearAddition(year) { let preLastDigit = Math.floor(year % 100 / 10); if (preLastDigit === 1) { return "лет"; } let lastDigit = year % 10; switch (lastDigit) { case 1: return "год"; case 2: case 3: case 4: return "года"; default: return "лет"; } } 

    • thanks a lot - Eugene Sukhomlyn

    Solution with moment.js Durations and i18n

     var years = [ 1822,1922,1948,1990,1995,1996,2008,2014,2016 ] years.forEach(function(year){ let yearsOld = moment().diff(moment([year]), 'year'); let yearsOldHumanized = moment.duration(yearsOld, 'years').humanize(); document.write(yearsOldHumanized + "<br>"); }); 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/ru.js"></script>