how to find the leap year - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# how to find the leap year

Posts: 7Member

## Comments

• Posts: 1,134Member
This function should do it ...

[code]
Private Function IsLeap(ByVal year As Integer) As Boolean
Return ((year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0))
End Function
[/code]
• Posts: 404Member
: This function should do it ...
:
: [code]
: Private Function IsLeap(ByVal year As Integer) As Boolean
: Return ((year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0))
: End Function
: [/code]
:
Hi,
here's another trick
[code]Public Function IsLeapYear(Yr As Integer) As Boolean
Return Date.TryParse(Yr & "-2-29")
End Function[/code]
but iwilld0it's solution is better! (Parsing date from string is time-consuming)
Faster than parsing (but with same idea) and slower than IsLeap is
[code]Private Function IsLeapYear(ByVal Yr As Integer) As Boolean
Return (New Date(Yr, 2, 28)).AddDays(1).Month = 2
End Function[/code]

[blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

[purple]Don't take life too seriously anyway you won't escape alive from it![/purple]

• Posts: 1,134Member
Here is a .NET centric way to do it w/out knowing anything about leap years:

[code]
Imports System.Globalization
' ...

Private Function IsLeapYear(ByVal yr As Integer) As Boolean
Return CultureInfo.CurrentCulture.Calendar.IsLeapYear(yr)
End Function
[/code]

This function is culture (language) aware.

: : This function should do it ...
: :
: : [code]
: : Private Function IsLeap(ByVal year As Integer) As Boolean
: : Return ((year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0))
: : End Function
: : [/code]
: :
: Hi,
: here's another trick
: [code]Public Function IsLeapYear(Yr As Integer) As Boolean
: Return Date.TryParse(Yr & "-2-29")
: End Function[/code]
: but iwilld0it's solution is better! (Parsing date from string is time-consuming)
: Faster than parsing (but with same idea) and slower than IsLeap is
: [code]Private Function IsLeapYear(ByVal Yr As Integer) As Boolean
: Return (New Date(Yr, 2, 28)).AddDays(1).Month = 2
: End Function[/code]
:
: [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
:
: [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]
:
:
:

Sign In or Register to comment.