On CBSSports.com: New Maxim Hometown Hotties here each day

Check for leap years in your VB.NET code with IsLeapYear

Tags: Microsoft development tools, Programming languages, .NET, Irina Medvinskaya, bLeapYear AsBoolean, leap year, IsLeapYear, Microsoft Visual Basic.Net, Visual Basic Tips Newsletter

  • Save
  • Print
  • Digg This
  • 1

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!

  • Save
  • Print
  • Digg This
  • 1

Print/View all Posts Comments on this article

Are they trying to tell us something?etruss@...  | 05/11/07
Do you plan on still being on the job then?chriswfl  | 05/11/07
Short sightedetruss@...  | 05/30/07
Check for leap years in your VB.NET code with IsLeapYearsaddo16@...  | 05/11/07

What do you think?

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

The Green Enterprise

advertisement
Click Here