1. Registered User
Join Date
Aug 2002
Posts
25

Anybody knows if there is a function that lets somebody know if a year is a leap-year in VB?

2. Registered User
Join Date
Nov 2002
Location
Ohio
Posts
90
Hi mvargasp,

No, there isn't a leap year function in VB. I usually use this

Private Function LeapYear(tmpDate As Date) As Boolean
Dim lclYear As Integer
'
' get year from date
'
lclYear = Year(tmpDate)
Select Case lclYear Mod 4
Case 0 ' might be leap year
If lclYear Mod 400 = 0 Then
LeapYear = False
Else
LeapYear = True
End If
Case Else
LeapYear = False
End Select

End Function

Good Luck,

3. SQL Consultant
Join Date
Apr 2002
Location
Posts
20,002
you forgot to divide by 100

1900 was not a leap year

4. Registered User
Join Date
Nov 2002
Location
Ohio
Posts
90
Thanks,

No...there is not a function or "easy" way to get leap years.

5. Registered User
Join Date
Nov 2002
Location
Ohio
Posts
90
Thanks,

No...there is not a function or "easy" way to get leap years.

6. Registered User
Join Date
Feb 2003
Location
San Antonio, TX
Posts
31
Originally posted by Bruce A. Baasch
Hi mvargasp,

No, there isn't a leap year function in VB. I usually use this

Private Function LeapYear(tmpDate As Date) As Boolean
Dim lclYear As Integer
'
' get year from date
'
lclYear = Year(tmpDate)
Select Case lclYear Mod 4
Case 0 ' might be leap year
If lclYear Mod 400 = 0 Then
LeapYear = False
Else
LeapYear = True
End If
Case Else
LeapYear = False
End Select

End Function

Good Luck,
Actually, there is a bug in that code. the test should be
Case 0 ' might be leap year
If lclYear Mod 400 = 0 Then
LeapYear = True
Else
LeapYear = False
End If

Every century that is NOT divisable by 400 is NOT a leap year.

7. Registered User
Join Date
Mar 2004
Posts
1

## Date Validation

Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"

8. Registered User
Join Date
Feb 2003
Location
San Antonio, TX
Posts
31

## Date Validation

[Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"]

Essentially, you have to perform the various tests that are already coded into IsDate.

If you work with the _text_ in the field, you can extract the MonthPortion, DayPortiuon, and YearPortion by locating the "/" or "-" characters and doing appropriate substring manipulations. (You can also identify invalid dates because of no separators.) Of course, you may have to allow the user to define the month, day, year field order (e.g. MM/DD/YYYY, YYYY-MM-DD, or DD/MM/YY).

Having performed those extractions, You can validate the YearPortion based upon two or four digit year rules (you'll have to define or allow the user to define the 2 digit year rule).

I would next validate the MonthPortion as being between 1 and 12, inclusive.

Based upon the month, you can validate the DayPortion with the only kink being the handling of Leap Years based upon the YearPortion and a MonthPortion of "2" or "02".

Were you asking for some specific code?

9. Purveyor of Discontent
Join Date
Mar 2003
Location
The Bottom of The Barrel
Posts
6,102

## Re: leap-year

Originally posted by mvargasp
Anybody knows if there is a function that lets somebody know if a year is a leap-year in VB?

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```
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```
Last edited by Teddy; 04-02-04 at 13:35.

10. Purveyor of Discontent
Join Date
Mar 2003
Location
The Bottom of The Barrel
Posts
6,102

## Re: Date Validation

Originally posted by nav66
Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"
This really isn't a function of a leap year, this is a function of whether or not the user entered a valid date. Why wouldn't you use the IsDate function?

Code:
```If IsDate(yourValue) = False Then
MsgBox "INVALID DATE"
End If```
Last edited by Teddy; 04-02-04 at 14:41.

#### Posting Permissions

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