Recipe 7.21. Checking for Leap Years


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.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net