Quote:
Originally posted by mvargasp
Anybody knows if there is a function that lets somebody know if a year is a leap-year in VB?
Thanks in advance.
|
Why do you need to do this?
I can't give a clear answer until I know the application, but I can give you a function that I regularly use to address real-world problems with leap years.
Actually, I have this implemented as a UDF in SQL Server 2k, but this is a working adaptation for use in
VB, it's essentially the exact same.:
Code:
Private Function DaysInYear(d As Date) As Integer
DaysInYear = DateDiff("d", "1/1/" & Str(year(d)), "1/1/" & Str(year(d)) + 1)
End Function
You could use this to answer your original question like so:
Code:
If DaysInYear(yourValue) = 366 Then
MsgBox yourValue & " occurs during a leap year."
End If
A practical application that I have to use this for on a daily basis is calculating figures based on pro-rated information.
Also, I was thinking, if you want it to return a yes/no for whether the date is in a leap year, that's a fairly straight-forward modification:
[code]Private Function IsLeapYear(d As Date) As Boolean
Dim i As Integer
i = DateDiff("d", "1/1/" & Str(year(d)), "1/1/" & Str(year(d)) + 1)
If i = 366 Then
IsLeapYear = True
Else
IsLeapYear = False
End If
End Function
New Usage:
Code:
If IsLeapYear(yourValue) Then
MsgBox yourValue & " is a leap year
End If