max days number in current Month

pdabasic

Active Member
Licensed User
Hello!

How can I check which is the maximum day number in the current month?
For example in DayInMonth(february,2012)= 29?

I tried thi sub but it doesn't work for me :(
Msgbox(DateDay(DateAdd(DateParse(month & "/01/" & DateYear(Now)), 0, +1, -1)))
 

Jost aus Soest

Active Member
Licensed User
Longtime User
B4X:
Sub LeapYear(Year As Int) As Boolean
  If Year Mod 4 <> 0 Then Return False
  If Year Mod 400 = 0 Then Return True
  If Year Mod 100 = 0 Then Return False
  Return True
End Sub

Sub DaysOfMonth(Year As Int, Month As Int) As Int
  Select Month
  Case 2
    If LeapYear(Year) Then Return 29 Else Return 28
  Case 4, 6, 9, 11
    Return 30
  Case Else
    Return 31
  End Select
End Sub
 
Last edited:

Basic4Life

Member
Licensed User
If you don't want to calculate it yourself and since DateTime is supported by the .Net Compact Framework, this should work too.

B4X:
   year = 2012
   month = 3
   
   
   'objDateTime is Door Object
   
   objDateTime.New1(False)
   objDateTime.CreateNew("System.DateTime" & objDateTime.System_Mscorlib)
   
   
   daysinmonth = objDateTime.RunMethod3("DaysInMonth",year,"System.Int32",month,"System.Int32")

   Msgbox(daysinmonth)
 

pdabasic

Active Member
Licensed User
Thank you for All but my function is work too but I did not remember I changed the dateformat.

So this works too:
DateFormat("yyyy.mm.dd.")
maxDayInMonth=DateDay(DateAdd(DateParse(DateYear(Now)&"."&month&".01."), 0, +1, -1))
 

mjcoon

Well-Known Member
Licensed User
Thank you for All but my function is work too but I did not remember I changed the dateformat.

So this works too:
DateFormat("yyyy.mm.dd.")
maxDayInMonth=DateDay(DateAdd(DateParse(DateYear(Now)&"."&month&".01."), 0, +1, -1))

I think you are doing what I was going to suggest, which was to add one to the current month (remembering to go from 12 to 1); go to 1st day of that month; subtract one day; get day number.

Mike.
 

pdabasic

Active Member
Licensed User
I think you are doing what I was going to suggest, which was to add one to the current month (remembering to go from 12 to 1); go to 1st day of that month; subtract one day; get day number.

Mike.

Sorry Mike I did not want to steal your merits!
It is not my function, i found it in the forum.
"my function" = function what I'm using for

Sorry, and thank you!
 
Top