0

I have been working with the following code but am a complete beginner when it comes to JS. I adapted this from the post ygssoni's code here, with Angela's edit for a more flexible date input.

How would I modify this to report someone's age in months and days at that moment, given their birth date?

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

  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) )
    ageString = "Only " + age.days + dayString + " old!";
  else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
    ageString = age.years + yearString + " old. Happy Birthday!!";
  else if ( (age.years > 0) && (age.months > 0) && (age.days == 0) )
    ageString = age.years + yearString + " and " + age.months + monthString + " old.";
  else if ( (age.years == 0) && (age.months > 0) && (age.days > 0) )
    ageString = age.months + monthString + " and " + age.days + dayString + " old.";
  else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
    ageString = age.years + yearString + " and " + age.days + dayString + " old.";
  else if ( (age.years == 0) && (age.months > 0) && (age.days == 0) )
    ageString = age.months + monthString + " old.";
  else ageString = "Oops! Could not calculate age!";

  return ageString;
}


alert(getAge('09/09/1989'));
Community
  • 1
  • 1
406LQE
  • 53
  • 1
  • 7
  • 2
    Please try something, then ask us why it doesn't work. – Jonathan M Aug 02 '16 at 19:50
  • Are you asking it to convert years to months? (i.e., instead of "2 Years, 2 Months, 3 Weeks" it would give "26 Months, 3 Weeks"?) – John Barton Aug 02 '16 at 19:55
  • @JonathanM Like I mentioned, I am a complete beginner when it comes to JS. I found this code and just got lucky that it was somewhat close to what I was looking for and actually worked. – 406LQE Aug 02 '16 at 19:56
  • @JohnBarton Yes, that's what I would like it to do. – 406LQE Aug 02 '16 at 19:59
  • I've put the code in JSBin to attempt editing it, but I get errors about variables already being defined and being used out of scope and it won't run. – 406LQE Aug 02 '16 at 20:05

2 Answers2

2

You should essentially remove all references to "years" in the latter parts of your code and re-write your large if/else section like this jsFiddle. The only parts I changed were lines 48 & lower:

var totMonths = age.months + 12 * age.years; 
if ( totMonths > 1 ) monthString = " months";
else monthString = " month";
if ( age.days > 1 ) dayString = " days";
else dayString = " day";


if ( (totMonths == 0) && (age.days > 0) ) {
    ageString = "Only " + age.days + dayString + " old!";
} else if ( (totMonths > 0) && (age.days == 0)) {
    ageString = totMonths + monthString + " old.";
} else if ( (totMonths > 0) && (age.days > 0))
   ageString = totMonths + monthString + " and " + age.days + dayString + " old.";
} else ageString = "Oops! Could not calculate age!";
John Barton
  • 155
  • 2
  • 8
0

Try this :

var dob = new Date("1/1/2016");
    var now = new Date();
    var timeMiliseconds = Math.abs(now.getTime() - dob.getTime());
    var days = Math.ceil(timeMiliseconds / (1000 * 3600 * 24)); 
    var monthsElapsed = Math.ceil(timeMiliseconds / (1000 * 3600 * 24 * 30))
    var daysElapsed = days % 30
    alert(monthsElapsed);
    alert(daysElapsed);
Raghvendra Kumar
  • 1,328
  • 1
  • 10
  • 26