Calculate the days of the month

Is there a way to calculate the number of days in a month?

+7
source share
6 answers

Yes:

Const July As Integer = 7 Const Feb As Integer = 2 ' daysInJuly gets 31. ' Dim daysInJuly As Integer = System.DateTime.DaysInMonth(2001, July) ' daysInFeb gets 28 because the year 1998 was not a leap year. ' Dim daysInFeb As Integer = System.DateTime.DaysInMonth(1998, Feb) ' daysInFebLeap gets 29 because the year 1996 was a leap year. ' Dim daysInFebLeap As Integer = System.DateTime.DaysInMonth(1996, Feb) 

Credit is sent to MSDN .

+17
source share

http://authors.aspalliance.com/aspxtreme/sys/DateTimeClassDaysInMonth.aspx

 Public Shared Function DaysInMonth ( _ ByVal year As Integer, _ ByVal month As Integer _ } As Integer 
0
source share
 Dim d As New DateTime(2010, 4, 1) Dim month As Integer = d.Month While d.Month = month Console.WriteLine(d.[Date]) d = d.AddDays(1) End While 

You can, of course, change the way the date is displayed to format it.

0
source share

Use an array: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] Add one to February if (year mod 400 = 0) or ((year mod 4 = 0) , not (year mod 100 = 0))

0
source share

To get the number of days of the current month

 Dim CurrentMonthDays As Int16 = DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month) 
0
source share

You have two simple solutions:

 5546>>m&1|30^(m==2)*2+(m==2&&y%4==0))) 

or

 (62648012>>m*2&3)+28+(m==2&&y%4==0))) 

where m is the day of the month and y is the year.

this solution is similar to the below array, but the array of bits is masked in magic numbers.

-one
source share

All Articles