0

HI,

I have the following code that is supposed to compare two dates:

 var d = ($('#day').val());
 var m = ($('#month').val() -1);
 var y = $('#year').val(); 
 var birthdate = new Date(y,m,d);
 alert('birthdate is' + birthdate);
 var today = new Date();
 alert('today is'+ today); 
 var diff = (today - birthdate);
 years = Math.floor(diff/(1000*60*60*24*365));
 alert(years);

It's basically working but I'm interested to see if the date of birth makes the user over 18 or not. So I've tried to put in 30th march 1993 - which would make the user 17. I'm alerting out the birthdate and it gives me back the correct date (mon mar 29 1993 00:00:00 GMT + 0100 BST)....however this is evaluating to 18 (alert(years) in the above code) when it should evaluate to seventeen. It's not until I put in 3rd April 1993 that it evaluates to 17.

Any ideas?

2
  • Leap years are mucking things up Commented Mar 29, 2011 at 13:32
  • See the great source of all knowledge for more information about calendar years. Commented Mar 29, 2011 at 13:37

4 Answers 4

2

You have to mind leap-years, timezones... before reinventing the wheel, I recommend that you use DateJS.

if((18).years().ago().isBefore(birthdate)) {
  // handle underage visitors
}
Sign up to request clarification or add additional context in comments.

4 Comments

For sure. Time and dates are messy and inconsistent. Hand rolled solutions involving them almost always include bugs. Much better to go with a well tested library if possible.
THis doesn't work. I just get and error saying (18).years().ago().isBefore is not a function...I do have the library included
This library is in alpha testing. I've just done some simple calculations with it and it is not reliable
My fault. isBefore was introduced after that alpha release. If using datejs it's best to use the latest version from https://github.com/datejs/Datejs.
1

That's because you forgot the leap years.

These years had 366 days and occur usually every four years, so in any 18 years there are about four days more than 365*18, thus moving the neccessary start date four days ahead.

Probably in this case it is easier to check

if ((nowyear - birthyear > 18) 
   || ((nowyear - birthyear == 18)&&(nowmonth - birthmonth > 0))
   || ((nowyear - birthyear == 18)&&(nowmonth == birthmonth)&&(nowday - birthday >= 0)))
  // you're 18!

2 Comments

Your last condition only checks that the months are equal and the current day is greater than the birthday, but doesn't check the years.
I don't know how to split the date up in to years and months. I have the birth year/month/day split up but how do i create todays date and get that in months/days/years?
1

If you're looking for age, why not just go the simple route and deal with years, months, and days?

function findAge( birthday ){
   var today = new Date();
   var age = today.getFullYears() - birthday.getFullYears();
   if( today.getMonth() - birthday.getMonth() < 0 ){
      age--;
   }
   else if( today.getDay() - birthday.getDay() < 0 && today.getMonth() == birthday.getMonth() ){
      age--;
   }
}

Comments

-1

try to take a look at this post

1 Comment

That doesn't answer the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.