-1

How do I calculate the age of a user, depending on the date entered in a textbox?

I want to be able to calculate the exact age of YYYY/MM/DD, so far I've only managed to do it by year.

The code I've tried so far:

function OnClick() {
var txtDate = $("#txtDate").val();
var date1 = new Date();
date1.setFullYear(txtDate);
var d1 = date1.getFullYear();
var date2 = new Date();
var d2 = date2.getFullYear();
var age = d2 - d1;
document.getElementById("Diven").innerHTML = age;

}

Any ideas?

Dr Cox
  • 149
  • 6
  • 35
  • How old is a person birth in 1st May 2000, and current date is 1st May 2013? How old is a person birth in Feb 29th 2000, while current date is Feb 28th 2010? – tsh Jun 04 '15 at 08:19
  • @tsh I don't want to hardcode this, I want to check the current age depending on what the textbox value is – Dr Cox Jun 04 '15 at 08:22
  • Yes, I know it is based on current date. I only want to clarify how to consider the age. Will 20101231 ~ 20121231 be considered as 2-year-old, or 3, or 1? And will 20000229 ~ 20020228 be considered as 2-year-old, or 3, or 1? Meaning of age is different from one region to another. – tsh Jun 04 '15 at 08:35
  • @tsh From where I see it, on your bithday date, you turn -your new age- so answer to your first question would be '3'. For leaplings, there is indeed regional differences for all legal/administrative purposes (and thus generally important only when you reach majority), but I don't think OP has any interest in this discussions and might just choose whether those people are considered "older" on the 28th of February or the 1st of March... – Laurent S. Jun 04 '15 at 08:46

4 Answers4

3

When you set the date using this: new Date(birthYear, birthMonth, birthDay); you need to subtract 1 from the month. Months are counted from 00.

For example,

var testDate = new Date(1988,10,12) is Sat Nov 12 1988 00:00:00

You can try this alternate way:

var today = new Date();
var inputBirthDate= new Date(birthYear, birthMonth - 1, birthDay);
var age = today.getFullYear() - inputBirthDate.getFullYear();
var month = today.getMonth() - inputBirthDate.getMonth();
if (month < 0 || (month === 0 && today.getDate() < inputBirthDate.getDate())) {
   age--;
}
console.log(age);

This will return you the correct age based on input birth date.

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
  • +1 cause that really looks really like the simplest answer. No complication, straight to the point, exactly how you calculate an age in real life, not with any division, modulo, floor,... – Laurent S. Jun 04 '15 at 08:39
  • @pramod-karandikar Thank you sir, as Laurent S said, this works very good and it's how you would do it in real life, sorry for accepting it a bit late, I was very stressed at the time and forgot to check stackoverflow. – Dr Cox Sep 30 '15 at 07:20
0

well since i can't make comment, im gonna do it here.

first thing i would do is use momentjs for every date releated thing in js

and if i understand your right, you want something like this?

How to get difference between 2 Dates in Years, Months and days using moment.js

Community
  • 1
  • 1
DaCh
  • 921
  • 1
  • 14
  • 48
  • beside i would recommend to use something that will force the user to give the right input in the textbox since it wouldn't work if the user input the date like 02-15-1968 since this use '-' instead of '/' And the date format is differencefrom what culture people are from. either something like a date mask, or split it up to 3 input could solve this. – DaCh Jun 04 '15 at 08:32
0

You can use Math.floor and the modulo operator % for integer division:

function CalculateAge() {
    var dob1 = $("#txtBirthday").val();
    var age1 = document.getElementById("#age");

    var dateAry = dob1.value.split("/");
    var birthDay = parseInt(dateAry[0]);
    var birthMonth = parseInt(dateAry[1]);
    var birthYear = parseInt(dateAry[2]);

    var birthDate = new Date(birthYear, birthMonth, birthDay);
    var currentDate = new Date();

    var age = currentDate - birthDate;  // don't forget the var keyword
    var days = Math.floor(age / 86400000); // no need to use a string
    var years = Math.floor(days / 365); // quite inaccurate actually
    var remaning_days = days % 365; // use modulo operator

    age1.value = years;  // no need to type-cast
}
Alsciende
  • 26,583
  • 9
  • 51
  • 67
-1
function CalculateAge() {
    var dob1 = $("#txtBirthday");
    var age1 = $("#age");
    var dateAry = dob1.val().split("/");
    var birthDay = parseInt(dateAry[0]);
    var birthMonth = parseInt(dateAry[1]);
    var birthYear = parseInt(dateAry[2]);

    var one_day=1000*60*60*24;
  var date1 = new Date(birthYear, birthMonth, birthDay);
  // Convert both dates to milliseconds
  var date1_ms = date1.getTime();
    var date2 = new Date();
  var date2_ms = date2.getTime();

  // Calculate the difference in milliseconds
  var difference_ms = date2_ms - date1_ms;

  // Convert back to days and return
  var t = difference_ms/one_day;
    age1.val(t/365);
}

if you want approx year use Math.random in result

Vivek Gupta
  • 955
  • 4
  • 7