MrExcel Publishing
Your One Stop for Excel Tips & Solutions

extracting the year out of a date variable


Posted by yu-kuan on August 09, 2000 5:50 PM

hi,

does anyone know how to extract the year value out of a date variable? for example, if i had like #3/15/99# stored in a date variable, i would like to have a function that would return #1999# as a date variable. i tried working w/ both the Year() and DatePart("y", var) and DatePart("yyyy", var) function, and none of them work.

for example:
dim YearDate as Date
dim CurrentDate as Date

CurrentDate = #3/15/99#
YearDate = Year(CurrentDate)

but YearDate becomes #1905/06/21#, what's going on?

thanks a lot
yu-kuan


Posted by David on August 09, 0100 11:02 PM

Tried you code and it didn't work.

yeardate cannot be defined as a date if you want an integer back.

Sub runme()
Dim YearDate As Date
Dim CurrentDate As Date
CurrentDate = #3/15/99#
david = DatePart("yyyy", CurrentDate)
End Sub