Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

Problem

You want to check a year to see if it's a leap year.

Solution

Use the shared IsLeapYear() function provided by the Date object to test any year.

Discussion

The IsLeapYear() function returns true if the year passed to it is a leap year and False if it isn't. The following code provides a working demonstration showing how to call this shared function to test the current year:

Dim leapYear As Boolean = Date.IsLeapYear(Now.Year) MsgBox(String.Format( _ "{0} is a leap year: {1}", Now.Year, leapYear))

Figure 7-22 shows the results as displayed by the message box.

Figure 7-22. The Date.IsLeapYear function reveals instantly that 2005 is not a leap year

Because the IsLeapYear() function is a shared function, you must call it directly from the Date object, not from an instance of a Date.

Категории