Recipe 7.17. Determining the Number of Days in a Month


Problem

You want to calculate the number of days in a given month.

Solution

Use the shared function DaysInMonth provided by the Date object. This function returns the number of days in a month, given the month and year.

Discussion

Unlike the DayOfWeek and DayOfYear properties available on every Date variable, DaysInMonth is a shared function. Instead of prefixing the call with a specific Date, use the generic Date object to access this function. The following code shows the correct syntax as it gets the number of days in the current month:

 Dim daysInMonth As Integer = _    Date.DaysInMonth(Now.Year, Now.Month) MsgBox(String.  Format( _    "Number of days in the current month: {0}", daysInMonth)) 

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

Figure 7-17. Determining the number of days in a given month with the shared function DaysInMonth





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