Today i am introducing the DaysInmonth() function to you that is the member of the System.date. With the help of this function you can find the count of the days in the specified month of the specified year.
You can use this function as:
System.Date.DaysInMonth(year As Integer, month As Integer)
where
year: The year.
month: The month (range between 1 to 12).
This function return the number of days in month for the specified year. For example, if month equals 4 for April the return value is 30 or 31 depending upon the year.
Example:
[vb]
Private Sub GetCount() Dim CountOfDays As Integer = GetCountOfDaysInMonth(2012, 4) MessageBox.Show("The number of days in April 2012 is " & CountOfDays) End Sub Private Function GetCountOfDaysInMonth(ByVal year As Integer, _ ByVal month As Integer) As Integer Dim countDays As Integer = Date.DaysInMonth(year, month) Return countDays End Function
[c#]
private void GetCount() { int CountOfDays = GetCountOfDaysInMonth(2012, 4); MessageBox.Show("The number of days in April 2012 is " + CountOfDays); } private int GetCountOfDaysInMonth(int year, int month) { int countDays = System.DateTime.DaysInMonth(year, month); return countDays; }
If given month is less than 1 or greater than 12 or year is less than 1 or greater than 9999 then it leads to excemption.