views:

52

answers:

3

Hi all,

I have my users DOB as a string (don't ask), how can I reliably check to see if they are over 18? Would I use DateDiff? How would I convert the string to a date first? The date is in dd/mm/yyyy format.

Many thanks Jonathan

A: 

Yep, you'll need CDate to convert your string and then DateDiff("yyyy",date1,date2) since you only really need the year.

LesterDove
I need to be accurate with the dob birth as I can't allow under 18s access, our users are all 17 or 18 so I'll need to be sure they are over 18. Thanks
Jonathan
I just noticed the other pain here, which is that CDate() might need some help with the locale-specific DD/MM/YYYY. For that, you could parse the constituent parts out and feed them back to CDate(mm.dd.yyyy)
LesterDove
That's exactly what I thought of first. Actually... I was wrong on the "yyyy" part. I thought it compared years... but rather, it specifies what is returned... I've deleted the comment for clarity.
Atømix
A: 

Something like:

stringDate = "02/12/1990"
if IsDate(stringDate) then
    document.write(DateDiff(yyyy, CDate(stringDate), Date()))
end if
Dustin Laine
A: 

VBScript's CDate function uses the current locale to convert the date. You may run into problems.

An alternative would be to do it the long way and parse the date out:

d = "02/05/1984"

sYear = cint(right(d,4))
sMonth = cint(mid(4,2))
sDay = cint(left(d,2))

ds = DateSerial(sYear, sMonth, sDay)

Now you can do your DateDiff:

if ( DateDiff( "yyyy", d, ds ) > 18 ) ...
Atømix