Jump to content

Recommended Posts

Hi Guys

 

I have a date of birth field using the jquery datepicker using the date format of dd/mm/yyyy, this is then used to calculate the users age, however using the code below and a date of 1st November 1978 (01/11/1978) then it calculate the age to be 33 and if I change the date to be 11/01/1978 then it calculates the age to be 32

 

        <script type="text/javascript">
                 function get_age(){
                   var today = new Date(), // today date object

      birthday_val = $("#dateofbirth").val().split('/'), // input value

      birthday = new Date(birthday_val[2],birthday_val[0]-1,birthday_val[1]), // birthday date object

      // calculate age

      age = (today.getMonth() == birthday.getMonth() && today.getDate() > birthday.getDate()) ?

            today.getFullYear() - birthday.getFullYear() : (today.getMonth() > birthday.getMonth()) ?

                  today.getFullYear() - birthday.getFullYear() :

                        today.getFullYear() - birthday.getFullYear()-1;



alert("Age: " + age);

}

        </script>

 

What would I need to change in the javascript above to use the date format of dd/mm/yyyy?

Link to comment
https://forums.phpfreaks.com/topic/249319-date-order/
Share on other sites

The range of dates is approximately 285,616 years from either side of midnight, January 1, 1970. Negative numbers indicate dates prior to 1970.

If the age is before 1970, you would need to split it and calculate the time before 1970 and add it to the time after 1970.

 

Also, a date picker is not a good tool for birthdates, I don't want to click back 100 times to get back to my birthdate.

Link to comment
https://forums.phpfreaks.com/topic/249319-date-order/#findComment-1280284
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.