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!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# how to find the leap year

Posts: 7Member

• 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]
:
:
: