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?

Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111

4 Answers4

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
}
Marcel Jackwerth
  • 53,948
  • 9
  • 74
  • 88
  • 2
    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. – Matt Greer Mar 29 '11 at 13:37
  • THis doesn't work. I just get and error saying (18).years().ago().isBefore is not a function...I do have the library included – Mike Rifgin Mar 29 '11 at 13:52
  • This library is in alpha testing. I've just done some simple calculations with it and it is not reliable – Mike Rifgin Mar 29 '11 at 13:59
  • 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](https://github.com/datejs/Datejs). – Marcel Jackwerth Mar 29 '11 at 14:15
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!
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
  • 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. – Adriano Varoli Piazza Mar 29 '11 at 13:39
  • 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? – Mike Rifgin Mar 29 '11 at 14:02
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--;
   }
}
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
-1

try to take a look at this post

Community
  • 1
  • 1
Faber
  • 2,194
  • 2
  • 27
  • 36