'jquery years months and days variable

when the system appears, I see that it is very complicated, but I have a problem, I found this code from somewhere, but I am writing because I do not have much knowledge, thank you in advance to those who help.

http://jsfiddle.net/8qbjh3z2/

function dateAgo(date) {  

    var startDate = new Date( date + " " + '12:00 AM');
    var endDate = new Date();

    var diffDate  = new Date(new Date() - startDate);

    var yearDiff  = diffDate.toISOString().slice(0, 4) - 1970;
    var monthDiff = diffDate.getMonth(); 
    var dayDiff   = diffDate.getDate()-1;

    var msec  = endDate - startDate;
    var mins  = Math.floor(msec / 60000);
    var hrs   = Math.floor(mins / 60);
    var days  = Math.floor(hrs / 24);
      
    var label = 'Posted ';
    if( mins <= 30 ){
        label += ' Just Now';
    }else if( hrs <= 24 ){
        label += ' Just Today';
    }else{
        if( yearDiff > 0  ){
            label += ( yearDiff > 1 ) ? yearDiff+' years, ' : yearDiff+' year, ';
        }

        if( monthDiff > 0 ){
            label += ( monthDiff > 1 ) ? monthDiff+' months, ' : monthDiff+' month, ';
        }

        if( dayDiff > 0 ){
            label += ( dayDiff > 1 ) ? dayDiff+' days ' : dayDiff+' day ';
        }

        label += 'ago';
    }
  
    return label;   
    
}

(function() {
    var elements = document.body.getElementsByClassName('iso-date');
    for (var i = 0; i < elements.length; i++) {
        var date  = elements[i].innerHTML;
        var label = dateAgo(date);
        elements[i].nextElementSibling.innerHTML = label;   
    }
})();
<div class="iso-date">2019-09-30</div>
<div class="display-date"></div>

The codes in the above code file are like this. The system I want to be done automatically adds the month, year or day when the date is changed. I want the week part to be added as an extra. If it is something that is not seen in the system, I want the system to be added in the class and id section that I have specified. I would appreciate your help.



Solution 1:[1]

function getAge(dateString) {
  var now = new Date();
  var today = new Date(now.getYear(),now.getMonth(),now.getDate());

  var yearNow = now.getYear();
  var monthNow = now.getMonth();
  var dateNow = now.getDate();

  var dob = new Date(dateString.substring(6,10),
                     dateString.substring(0,2)-1,                   
                     dateString.substring(3,5)                  
                     );

  var yearDob = dob.getYear();
  var monthDob = dob.getMonth();
  var dateDob = dob.getDate();
  var age = {};
  var ageString = "";
  var yearString = "";
  var monthString = "";
  var dayString = "";


  yearAge = yearNow - yearDob;

  if (monthNow >= monthDob)
    var monthAge = monthNow - monthDob;
  else {
    yearAge--;
    var monthAge = 12 + monthNow -monthDob;
  }

  if (dateNow >= dateDob)
    var dateAge = dateNow - dateDob;
  else {
    monthAge--;
    var dateAge = 31 + dateNow - dateDob;

    if (monthAge < 0) {
      monthAge = 11;
      yearAge--;
    }
  }

  age = {
      years: yearAge,
      months: monthAge,
      days: dateAge
      };

  if ( age.years > 1 ) yearString = " years";
  else yearString = " year";
  if ( age.months> 1 ) monthString = " months";
  else monthString = " month";
  if ( age.days > 1 ) dayString = " days";
  else dayString = " day";


  if ( (age.years > 0) && (age.months > 0) && (age.days > 0) )
    ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old.";
    else if ( (age.years == 0) && (age.months == 0) && (age.days > 0) );
  else if ( (age.years > 0) && (age.months > 0))
    ageString = age.years + yearString + " and " + age.months + monthString + " old.";
 
  else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
    ageString = age.years + yearString ;
  else if ( (age.years == 0) && (age.months > 0))
    ageString = age.months + monthString + " old.";
  else ageString = "Oops! Could not calculate age!";

  return ageString;
}

document.getElementById('test').innerHTML += "<br />" + (getAge('02/09/2019'));
<div id="test">

</div>

There was a solution in this way, but I could not do this. The system, for example: 2 years, 11 months, and 30 days old. gives. I want the registration date in the system to be 1 year and 2 months after 1 year, so I cannot cancel the number of days.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Musa PEKEL