www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: date of birth to age converter?

  1. #1
    Join Date
    Dec 2005
    Posts
    9

    date of birth to age converter?

    Hi everyone.

    I'm trying to incorporate a date of birth to age calculator on my site. Currently I have the following Date Validation which validates the date of birth to make sure it's in the dd/mm/yyyy format.

    Then, I have the following piece of code, which calculates the age, according to the date of birth

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function calc(form) {
    	var today= new Date();
    	var birthday= new Date(document.form1.dob.value);
    	secold=(today.getYear()-birthday.getYear())*31557600+(today.getMonth()-birthday.getMonth())*2629800+(today.getDay()-birthday.getDay())*86400+today.getHours()*3600+today.getMinutes()*60+today.getSeconds()
    
    	document.form1.age.value=Math.floor(((secold/3600)/24)/365.25);
    }
    //-->
    </script>
    My problem is, this age calculator expects the date format to be mm/dd/yyyy. Is there a way of specifying that my date in the the format of dd/mm/yyyy?

  2. #2
    Join Date
    Mar 2003
    Posts
    575
    First of all:

    This convertor is bull****. Look at it! Just LOOK at the silly thing!

    I once wrote a beautiful function to convert dd/mm/yyyy dates to indices, so you could subtract the indices and determine the number of dates between dates.

    http://www.webdeveloper.com/forum/sh...ad.php?t=87871

    Second:

    You probably want years, not days or seconds. For years, you don't even need the Date() object, except to grab today's date. And remember, JavaScript month numbering is funny: in JavaScript, January is month 0, February is month 1, etc. So add 1 to the JavaScript month number to get the month number as used by laymen. EXCEPTION: read the note at the bottom of this message!

    If you want seconds: Feed the dates into Date() objects (remember to allow for the funny month numbering!), then subtract these Date() objects to get milliseconds. Divide this number by 1000 to get seconds (NB: this number will probably not be an integer unless you round it).

    Third:

    I'm not through telling you about the Date() object. If you give it a date as a string, it will evaluate it with layman's month numbering: new Date("01/01/2006") gives you New Year's Day 2006. But if you give it a date as numbers, the eccentric month numbering is used: new Date(2006,1,1) gives you Feb. 1, 2006.

  3. #3
    Join Date
    Dec 2005
    Posts
    9
    Well, unfortunattely I'm not too good with JS, still learning so this is all new to me.

    The problem I have is, I want to calculate someone's age, given a date of birth, and I need it to be accurate.

    If today's date = 19 Jan. 2006 (19/01/2006), and the person was born on the 20/01/2005 that should theoretically make him 2 years old (since he's in his 2nd year), right? Which is why I can't just work on the year field, but I also need the day, and month field's values.

    I was born on the 10 Nov 1979. If I take 2006 - 1979, I get 27, though I'm only 26. And this is why I need an acurate calculator. I had a look @ your script though I'm not quite sure how to use it. How would I convert the current Date() format, which is mm/dd/yyyy to dd/mm/yyyy with that script, or a modified one?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Would this code help you?:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    function 
    showAge(){
    var 
    =document.getElementById('txt').value.split('/');
    var 
    today=new Date();
    var 
    bday=new Date(d[2],d[1],d[0]);
    var 
    by=bday.getFullYear();
    var 
    bm=bday.getMonth()-1;
    var 
    bd=bday.getDate();
    var 
    age=0; var dif=bday;
    while(
    dif<=today){
    var 
    dif = new Date(by+age,bm,bd);
    age++;
    }
    age +=-;
    alert('You are '+age+' years old')
    }
    </script>
    </head>
    <body>
    Insert your date of birth in format dd/mm/yyyy<br>
    <input id="txt" type="text"><input type="button" value="Show Age" onclick="showAge()">
    </body>
    </html> 
    Last edited by Kor; 01-20-2006 at 02:33 AM.

  5. #5
    Join Date
    Mar 2003
    Posts
    575
    Kor, you're supposed to subtract 1 from the month _before_ feeding the date into the Date() object.

    Kor, what system do you suppose the Date() object really _does_ use to store dates internally? (This isn't a lame question: internal storage formats _can_ at times affect results, as with floating point arithmetic.)

    LordMerlin, a person's age (as is usually given) changes on their birthday, and not a day before. A person legally becomes 18 years old on his or her 18th birthday, not a day before. And yes, you do need the values for the day and the month.

  6. #6
    Join Date
    Mar 2003
    Posts
    575
    Here's a hint, Lord Merlin: Use if statements.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Juuitchan
    Kor, you're supposed to subtract 1 from the month _before_ feeding the date into the Date() object.
    Why before?
    var bm=bday.getMonth()-1
    do that anyway
    Kor, what system do you suppose the Date() object really _does_ use to store dates internally? (This isn't a lame question: internal storage formats _can_ at times affect results, as with floating point arithmetic.)
    Yes. CPU has an internal built-in date calendar. The return of a Data() object is in fact a number, milliseconds) so that it is easy to compare bitwise two dates.

    My code works, try it with any resonable date birth...
    Last edited by Kor; 01-19-2006 at 03:21 PM.

  8. #8
    Join Date
    Mar 2003
    Posts
    575
    Okay. Try January 31, 2000 as the birthday and see what happens.
    (Note: you will notice this if your system clock is set to Feb. 1.)

    Kor, I thought you were smarter than that.

    (fixed typo)
    Last edited by Juuitchan; 01-19-2006 at 05:32 PM.

  9. #9
    Join Date
    Mar 2003
    Posts
    575
    The *obvious* way to do it is this:

    - Get the birthday and also get today's date.
    - Subtract the year of the birthday from the year of today's date and store this number in a temporary variable.
    - If the month of the birthday is greater than the month of today's date, decrease the temporary variable by 1. (NB: you will have to use the same month numbering scheme for the birthday as for today's date.)
    - If the month of the birthday matches the month of today's date, then check to see if the day-of-the-month of the birthday is greater than the day-of-the-month of today's date, and if it is, decrease the temporary variable by 1.
    - the temporary variable now holds the correct age.

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Juuitchan
    Okay. Try January 31, 2000 as the birthday and see what happens.
    (Note: you will notice this if your system clock is set to Feb. 1.)

    Kor, I thought you were smarter than that.

    (fixed typo)
    ? It will show correctly 5 years, if checking now, and 6 years if seting the internal clock to 01 Feb 2006... I really don't see which is your problem.... Do we talk about the same code? As long as you need to alert only the years difference, my code will do that with no error. I might be blind or something, but where is the error?

  11. #11
    Join Date
    Mar 2003
    Posts
    575
    Okay. I'm a JavaScript interpreter.
    The input field holds the string "31/01/2000", and (for testing purposes) I am using 01 Feb 2006 (obviously not the real date) as today's date.
    I will run showAge().
    Code:
    function showAge(){ 
    var d =document.getElementById('txt').value.split('/');
    // now d is the array ['31','01','2000'] 
    var today=new Date(); 
    // The year, month, and day of the variable today
    // are 2006, 1, and 1, respectively.
    // Remember, I am a JavaScript interpreter.
    var bday=new Date(d[2],d[1],d[0]);
    // The year of bday is 2000.
    // The month of bday would be 1 (remember, to me, 1 is February)
    // except that the given date (31) is too large for February
    // so we go into March -- the 2nd day of March, to be exact.
    // To me, March is month 2.
    // So, the month and day of bday are 2 and 2, respectively. 
    var by=bday.getFullYear(); // this is 2000. 
    var bm=bday.getMonth()-1; // this is 1 (because 2-1=1)
    var bd=bday.getDate(); // this is 2
    var age=0; var dif=bday; 
    while(dif<=today){ 
    var dif = new Date(by+age,bm,bd); 
    age++; 
    } 
    // so you loop with the 2nd of February as the date of birth!!
    age +=-2 ; 
    // what is the above line supposed to do?
    alert('You are '+age+' years old') 
    }

  12. #12
    Join Date
    Mar 2003
    Posts
    575
    Let me try this for real. My system date is now 20 Jan 2006.

    With birthday "19/01/2000": age is 6. Okay.
    With birthday "20/01/2000": age is 6. Okay.
    With birthday "21/01/2000": age is 5. Okay.

    Replacing var today=new Date(); with var today=new Date("Feb 1, 2006");
    (I don't want to screw up my clock):

    With birthday "31/01/2000": age is 5. Bad!!
    Last edited by Juuitchan; 01-20-2006 at 03:42 AM.

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Ok, here's a variant which migh pleased you:
    PHP Code:
    <script type="text/javascript">
    function 
    showAge(){
    var 
    today=new Date();
    var 
    =document.getElementById('txt').value.split('/');
    var 
    by Number(d[2]); var bm Number(d[1])-1; var bd Number(d[0]);
    var 
    bday = new Date(by,bm,bd)
    var 
    age=0; var dif=bday;
    while(
    dif<today){
    var 
    dif = new Date(by+age,bm,bd);
    age++;
    }
    age +=-;
    alert('You are '+age+' years old')
    }
    </script> 

  14. #14
    Join Date
    Dec 2005
    Posts
    9

    Question confused???

    Ok, I see the problems discussed (using the sample code provided), but how does one get around it? Will it only be a problem on the 1st Feb, and will it happen each year on the 1st Feb?

    What if one then writes a small function to check if the PC / user's date is on the 1st FEB, and then add 1 to the year given? will that work?

  15. #15
    Join Date
    Mar 2003
    Posts
    575
    You don't need the Number function; subtracting zero works.

    And you don't need a loop either.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles