20

I need to calculate if someone is over 18 from their date of birth using JQuery.

var curr = new Date();
curr.setFullYear(curr.getFullYear() - 18);

var dob = Date.parse($(this).text());

if((curr-dob)<0)
{
    $(this).text("Under 18");
}
else
{
    $(this).text(" Over 18");
}

There must be some easier functions to use to compare dates rather than using the setFullYear and getFullYear methods.

Note: My actual reason for wanting to find a new method is length of the code. I have to fit this code into a database field that is limited to 250 chars. Changing the database is not something that can happen quickly or easily.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
Robin Day
  • 100,552
  • 23
  • 116
  • 167
  • Why isn't your DateTime field in DB just take in the current date and you calculate the age in your code. – TStamper Mar 18 '09 at 14:50
  • This is a hack to show age in the GUI without any changes to any code or database. I will do it correctly when a release can be planned in. – Robin Day Mar 18 '09 at 14:59

7 Answers7

25

You might find the open source Datejs library to be helpful. Specifically the the addYears function.

var dob = Date.parse($(this).text());
if (dob.addYears(18) < Date.today())
{
    $(this).text("Under 18");
}
else
{
    $(this).text(" Over 18");
}

In a more terse fashion:

$(this).text(
    Date.parse($(this).text()).addYears(18) < Date.today() ?
    "Under 18" :
    " Over 18"
)
Ken Browning
  • 28,693
  • 6
  • 56
  • 68
9
Date.prototype.age=function(at){
    var value = new Date(this.getTime());
    var age = at.getFullYear() - value.getFullYear();
    value = value.setFullYear(at.getFullYear());
    if (at < value) --age;
    return age;
};

var dob = new Date(Date.parse($(this).text()));

if(dob.age(new Date()) < 18)
{
    $(this).text("Under 18");
}
else
{
    $(this).text(" Over 18");
}
Dónal
  • 185,044
  • 174
  • 569
  • 824
David
  • 19,389
  • 12
  • 63
  • 87
  • 1
    +1 Great answer, used it answering this question http://stackoverflow.com/questions/7210887/custom-unobtrusive-date-validators-for-dates/7214675#7214675 – Robin van der Knaap Aug 27 '11 at 12:59
3

You can remove the separate variable for DOB and collapse the if statement. The below code comes in at 165 characters:

var check = new Date();
check.setFullYear(check.getFullYear() - 18);
$(this).text((new Date("3/6/2009").getTime() - check.getTime() < 0)?"Under 18":"Over 18");

This will still keep the logic needed to deal with leap-years.

Parrots
  • 26,658
  • 14
  • 59
  • 78
1

keep in mind that all above answers work only for date using separator '/'. if you are using other that this then you have to replace that separator first.

var startDate = $('#start_date').val().replace('-','/');
var endDate = $('#end_date').val().replace('-','/');

if(startDate > endDate){
   // do stuff here...
}

happy coding :D

Vikash Singh
  • 804
  • 1
  • 10
  • 11
1
$(this).text(((new Date().getFullYear()-Date.parts($(this).text()))>=18)?"Over 18":"Under 18");

Better? :D

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
  • This only seems to use the year field for calculation, won't take into account months/days of current date and the DOB. So with this someone born in Dec 1991 would come back as over 18 in Jan 2009, which isn't true (they'd be 18 in Dec 2009). – Parrots Mar 18 '09 at 15:04
  • yes... it won't take into effect the months and days.. using a library would be useful if you want the full functionality, like in the first answer. Still can be built, though it might be difficult inside of 250 characters. – Sudhir Jonathan Mar 22 '09 at 16:42
0

You could use the Date object. This will return the milliseconds between the two dates. There are 31556952000 milliseconds in a year.

function dateDiff(var now, var dob)
{
    return now.getTime() - dob.getTime();
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Shaun Humphries
  • 1,070
  • 9
  • 15
0

My solution.

var startDt=document.getElementById("startDateId").value;
var endDt=document.getElementById("endDateId").value;
if( (new Date(startDt).getTime() > new Date(endDt).getTime()))
{
    ----------------------------------  
}
eeerahul
  • 1,629
  • 4
  • 27
  • 38
samba
  • 621
  • 5
  • 2