Sub DaysInMonth(Mth As Int, Year As Int) As Int
Dim Days, DaysPerMonth() As Int
DaysPerMonth = Array As Int (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
Days = DaysPerMonth(Mth)
If Mth = 1 AND (Year Mod 4 = 0) AND Not(Year Mod 100 = 0) Then
Days = 29
End If
Return Days
End Sub
Another one to add to DateTime. in the future.
Pass the month and year and return the number of days in that month - taking into account leap years. 0 based.
Thanks to Klaus - extracted form his code in ClsWheel....
------------------------------------
I need to populate the selected month of a calendar with either week day events OR weekend events in a batch. It should not be difficult to figure out which integer days are week days or weekends...
------------------------------------
The year 2000 was a leap year (as every year dividably by 400)!
I suggest using this function:
B4X:
Sub IsLeapYear(Year As Int) As Boolean
If Year Mod 400 = 0 Then Return True
If Year Mod 100 = 0 Then Return False
If Year Mod 4 = 0 Then Return True
Return False
End Sub
I prefer letting the OS/compiler/language do the job so I don't have to think about leap years.
The idea is to go to the first day of the next month and then substract one day.
The code uses AHLocale library for date parsing and formatting since the B4A DateTime functions are not thread safe. The same result is possible with B4A DateTime functions.
B4X:
Sub DaysInMonth(Mth As Int, Year As Int) As Int
Dim timeStamp As Long
Dim dt As AHDateTime
dt.Initialize
dt.pattern="d-M-yyyy"
timeStamp = dt.Parse("1-" & Mth & "-" & Year)
timeStamp = DateTime.Add(timeStamp, 0, 1, -1)
dt.Pattern = "d"
Return dt.Format(timeStamp)
End Sub