Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Age calculation on date

Tags:

Am I missing something in the following jQuery code?

var dob = $('#date').val();
if(dob != ''){
    var today = new Date();
    var dayDiff = Math.ceil(today - dob) / (1000 * 60 * 60 * 24 * 365);
    var age = parseInt(dayDiff);
    $('#age').html(age+' years old');
}

I am getting the pre-fetched value of #date from MySQL db.

<input type="text" value="1988-04-07" id="#date" name="dob" /><p id="age"></p>

It's returning NaN, not the correct value.

like image 790
seoppc Avatar asked Apr 02 '11 17:04

seoppc


Video Answer


2 Answers

$('#date').val() returns the string '1988-04-07'. You need to parse it into an actual number.

dob = new Date(dob);
var today = new Date();
var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
$('#age').html(age+' years old');

As @esqew points out, you also need to change id="#date" to id="date".

like image 62
Matt Ball Avatar answered Sep 19 '22 09:09

Matt Ball


function getAge(dateString) {
  var now = new Date();
 
  var yearNow = now.getFullYear();
  var monthNow = now.getMonth();
  var dateNow = now.getDate();
 //date must be mm/dd/yyyy
  var dob = new Date(dateString.substring(6,10),
                     dateString.substring(0,2)-1,                   
                     dateString.substring(3,5)                  
                     );
 
  var yearDob = dob.getFullYear();
  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;
}
 
// A bit of jQuery to call the getAge() function and update the page...
$(document).ready(function() {
  $("#submitDate").click(function(e) {
    e.preventDefault();
 
    $("#age").html(getAge($("input#date").val()));
 
  });
});

and HTML IS
like image 27
Dinesh Rabara Avatar answered Sep 22 '22 09:09

Dinesh Rabara