Jump to content

Date Order


bravo14

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.