jquery years months and days variable

61
March 16, 2022, at 5:40 PM

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.

Answer 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.

READ ALSO
Jquery Confirm a form after Validation

Jquery Confirm a form after Validation

I have a html form which I validate with JqueryWhat I want to do now is to let the user review his form and to confirm sending the form after it was validatet by jquery

106
Find element that&#39;s on the middle of the visible screen (viewport) to target another element

Find element that's on the middle of the visible screen (viewport) to target another element

What I am aiming to achieve is something that looks like thisyou can see it in action on this URL, if you scroll a bit

65
How to trigger child elements continuous

How to trigger child elements continuous

depending on which container I click, I would like to add a class to the equal boxSo if I click on the first container -> add class to first box

67
Check checkbox by index in another list

Check checkbox by index in another list

I have an unordered list with div and a hidden checkbox list, each with the same number of items

99