Playing with DateDiff

1. Senior Member
Join Date
Dec 1969
Posts
1,030

## Playing with DateDiff

I thought the code below should return the number of years. It is returning 14138. What am I missing? Trying to learn DateDiff carachteristics.<BR><BR> DOB = #2/9/1965# <BR> Years = DateDiff("y", DOB, Date()) <BR> If Date() &#062; DOB Then Years = Years - 1 <BR> response.write Years

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## datediff("yyyy", dob , date)

datediff("yyyy", dob , date)

3. Senior Member
Join Date
Dec 1969
Posts
1,030

## Sheesh

Rookie mistake.<BR><BR>Thanks White Knight

4. Senior Member
Join Date
Dec 1969
Posts
1,030

## OOps

Trying get an age based on birthdate. I know now that the code works that I only am comparing year. Doesn&#039;t work does it? How would you compare month and day to current month and day to see if the persons birthday has happened yet.<BR>

5. Senior Member
Join Date
Dec 1969
Posts
1,030

## Double OOps

Should have posted what I am trying huh?<BR>Dim DOB <BR> DOB = #9/29/1953# <BR> <BR>Dim Years <BR> Years = DateDiff("yyyy", DOB, Date()) <BR> If month(Date()) AND day(date()) &#060; month(DOB) and day(DOB) Then Years = Years - 1 <BR> response.write Years

6. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## RE: Double OOps

[code language="VBScript"]Dim DOB, _<BR> BirthdayThisYear, _<BR> Age<BR><BR> DOB = #05/20/1980#<BR> BirthdayThisYear = DateSerial(Year(Now()), Month(DOB), Day(DOB))<BR><BR>Response.Write "DOB = " & DOB & "&#060;br /&#062;" & vbCrLf<BR>Response.Write "Birthday (this year) = " & BirthdayThisYear & "&#060;br /&#062;" & vbCrLf<BR><BR>Age = DateDiff("yyyy", DOB, Date())<BR><BR>If Date() &#060; BirthdayThisYear Then<BR> Response.Write "*** Birthday hasn&#039;t happened this year.***&#060;br /&#062;" & vbCrLf<BR> Age = Age - 1<BR>End If<BR>Response.Write "Age = " & Age & "&#060;br /&#062;"[/code]

7. Senior Member
Join Date
Dec 1969
Posts
1,030

## Hey now...

I was getting there! LOL<BR><BR>I&#039;ll study this and figure out what you are doing here.<BR><BR>Thanks!

8. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## It's all pretty easy.

Line by line:<BR><BR>Declare local variables. I&#039;m spastic about this and surprised that I didn&#039;t have "Option Explicit" at the top of the example script.<BR>[code language="VBscript"]Dim DOB, _ <BR> BirthdayThisYear, _ <BR> Age[/code]<BR><BR>Give the date of birth variable a value.<BR>[code language="VBScript"]DOB = #05/20/1980#[/code]<BR><BR>Get a date that has the Month & Day from the DOB, but the year from the current date.<BR>[code language="VBScript"]BirthdayThisYear = DateSerial(Year(Now()), Month(DOB), Day(DOB))[/code]<BR><BR>Just some debugging.<BR>[code language="VBScript"]Response.Write "DOB = " & DOB & "&#060;br /&#062;" & vbCrLf<BR>Response.Write "Birthday (this year) = " & BirthdayThisYear & "&#060;br /&#062;" & vbCrLf[/code]<BR><BR>Get the number of years difference between the date of birth and the current year.<BR>[code language="VBScript"]Age = DateDiff("yyyy", DOB, Date())[/code]<BR><BR>If the current date is less than the birthday date (for this year), then we want to subtract one year.<BR>[code language="VBScript"]If Date() &#060; BirthdayThisYear Then <BR> Response.Write "*** Birthday hasn&#039;t happened this year.***&#060;br /&#062;" & vbCrLf <BR> Age = Age - 1 <BR>End If[/code]<BR><BR>Again, more debugging.<BR>[code language="VBScript"]Response.Write "Age = " & Age & "&#060;br /&#062;"[/code]

9. Senior Member
Join Date
Dec 1969
Posts
1,030

## Yep

Looked it all over and came back to say thanks for the lesson and found this. It really is pretty easy once you take a look at the code.<BR><BR>Thank you kin sir.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•