2010-04-20 98 views
7

有沒有計算一個月內天數的方法?計算一個月中的天數

+2

當然,這是30.6 – 2010-04-20 14:13:58

+2

其實,它更接近30.436875平均。 400年內有((365 * 3 + 366)* 25-1)* 4 + 1或146,097天。 – paxdiablo 2010-04-20 14:22:15

回答

17

是:

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) 

幸得MSDN

+0

如果你正在尋找的天數,這個工程。如果你正在尋找一個天數列表,請使用下面的示例。 – NibblyPig 2010-04-20 14:12:31

0
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 

當然你可以改變的日期如何輸出對其進行格式化,以你的意志。

0

使用數組:[31,28,31,30,31,30,31,31,30,31,30,31] 如果(year mod 400 = 0)或((year MOD 4 = 0),而不是(年MOD 100 = 0))

-1

有兩種簡單的解決方案:

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

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

其中m是月和y的數量是那一年。

這個解決方案下面是一樣的數組,但是數組是用幻數掩蓋的。

0

要獲得當月的天數

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