bravo14 Posted October 18, 2011 Share Posted October 18, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249319-date-order/ Share on other sites More sharing options...
nogray Posted October 18, 2011 Share Posted October 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249319-date-order/#findComment-1280284 Share on other sites More sharing options...
bravo14 Posted October 18, 2011 Author Share Posted October 18, 2011 Is there a recommended way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/249319-date-order/#findComment-1280325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.