Check for leap years in your VB.NET code with IsLeapYear
Takeaway: Whenever you need to determine if a year is a leap year in VB.NET, you can use the IsLeapYear function. This tip shows how you can use this function in your development work.
When dealing with dates in VB.NET, it's a good idea not to perform manual checks or calculations, which may be inaccurate depending on the quality of the code. Instead, it's advisable to rely on the functionality of classes provided by .NET.
For instance, if you need to determine if a given year is a leap year, you can use VB.NET's IsLeapYear function. Here's an example of how you can use the function:
Private Sub LeapYearCheck()
Dim bLeapYear AsBoolean
bLeapYear = Date.IsLeapYear(Now.Year)
MessageBox.Show(bLeapYear)
bLeapYear = Date.IsLeapYear(2004)
MessageBox.Show(bLeapYear)
End Sub
In the example, I define a Boolean variable, bLeapYear, to hold the result of whether a given year is a leap year. I then set the value of bLeapYear to the IsLeapYear property of the Date class and pass to it the current year, which I obtain using the Year property of the Now class. I show the value of bLeapYear in a MessageBox. The result is False because 2007 is not a leap year. After that, I follow the same steps for 2004, which is a leap year. In that case, MessageBox shows True.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- Now is the Time for HD Video Communications - The Smart Business Tool GBH Communications
- Best Practices in Creating a Strategic Finance Function SAP
- MIT Podcast: How Enterprise Software Drives Higher Productivity Efficiency SAP
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


Harnessing the power of waves
Planting solar gardens
Fill your car for $1.10 a gallon?
