Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help with "date of birth" to "age" calculation

    Hi everyone.

    I'm trying to incorporate a date of birth to age calculator on my site. Currently I have the following Date Validation Script 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
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One thing you could do is it split the data into the different sections then join them back up into the american format.

    i.e.
    Code:
    $splitdate = birthday.split("/")
    
    $american_date = $splitdate[1]+"/"+$splitdate[0]+"/"+$splitdate[2]

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Here's a shorter way to do it that does both the validation of the date format and the age calculation all at once. The getYear method that your code uses returns different values in different browsers because it is NOT Y2K compliant, the following code doesn't have that problem because it compares the dates directly.

    Code:
    <script language="javascript" type="text/javascript">
    function calc(form1) {
    var bd = form1.dob.value;
    var re = /^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[0-2])\/(19|20)\d{2}$/;
    if (!re.test(bd)) {alert('Birthday must be dd/mm/ccyy format'); return;}
    $sd = bd.split("/")
    var today= new Date();
    var birthday= new Date(sd[2],sd[1],sd[0]);
    var age = today - birthday;
    form1.age.value=Math.floor(((age/3600)/24)/365.25);
    }
    </script>
    Last edited by felgall; 01-19-2006 at 11:11 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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