Calculate the last day of a month

mindbox

Member
Licensed User
Longtime User
How can I calculate the last date of a month?

In Visual Basic I used DateSerial, like this:
B4X:
Private Function GetLastDayOfMonth(pDate As Date)
     GetLastDayOfMonth = Day(DateSerial(Year(pDate), Month(pDate) + 1, 0))
End Function

How can I do this in B4A?
 

mc73

Well-Known Member
Licensed User
Longtime User
If I understand correctly, why not setting an array, like
B4X:
dim lD(12) as int
ld(0)=31:ld(1)=28:ld(2)=30
'and so on
and adjusting february
B4X:
if year mod 4=0 then ld(1)=29
though I am sure there is a way using dateTime formulas
 
Upvote 0

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 100 = 0 Then Return Year Mod 400 = 0
   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
 
Upvote 0

mindbox

Member
Licensed User
Longtime User
Thanks, mc73!

I've done it like this:

B4X:
Sub GetLastDayOfMonth(sDate As String) As String
   Dim dtDate As Long
   DateTime.DateFormat = "dd.MM.yyyy"
   dtDate = DateTime.DateParse(sDate)
   Select Case DateTime.GetMonth(dtDate)
        Case 1, 3, 5, 7, 8, 10, 12
            Return "31"
        Case 4, 6, 9, 11
            Return "30"
        Case 2
            If (DateTime.GetYear(dtDate) Mod 4) = 0 Then
            If (DateTime.GetYear(dtDate) Mod 100) = 0 Then
               If (DateTime.GetYear(dtDate) Mod 400) = 0 Then
                  Return "29"
               Else
                  Return "28"
               End If
            Else
               Return "29"
            End If             
            Else
             Return "28"
            End If
    End Select
End Sub

Late Edit: Thanks, Jost aus Soest!

But, there must be a more elegant way..
 
Last edited:
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Leap Years aren't always mod 4, there is more to the calculation (As Jost pointed out). Most date functions are pretty smart. I haven't used them yet in B4A, but I know in other languages and even databases that you can usually do something simple like going to the first day of the next month and subtracting one day. Some are even smart enough to correct errors like setting the date to June 31st and it sets it to July 1st, etc.

Added:
Just Looked at the Date Functions and didn't see much of a subtract, but it does use ticks which you can subtract a days worth and then return that date.
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps you could try this:
B4X:
ld=30+expr(month<8)*(month Mod 2)+expr(month>=8)*((month+1) Mod 2)-expr(month=2)*(2-expr(year Mod 4 = 0)*(expr(year Mod 100 =0)*expr(year Mod 400 =0)+expr(year Mod 100>0)))

Sub expr(expression As Boolean) As Int 
If expression=True Then
Return 1
Else
Return 0
End If
End Sub

though I still think that dimming uses less typing :)
 
Upvote 0

mddmx

Member
Licensed User
Longtime User
Here's a quick stab at this.

Dim DT1 As Long
DT1 = DateTime.DateParse("03/01/2012")
DT1 = DT1 - DateTime.TicksPerDay
Msgbox(DateTime.GetDayOfMonth(DT1), "")
 
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
Get the Internet date.
Change the day of that date to 1.
Add 1 month to that date.
Subtract 1 day from that date.

DateTime - Basic4android Wiki


http://www.b4x.com/forum/bugs-wishlist/15447-wish-datetime-add.html#post87631

Doesn't look like b4a has the date add routines from VB6.

Bummer.

No wait, silly me .add is better than the VB6 date routines. LOL

So:



Add (Ticks As Long, Years As Int, Months As Int, Days As Int) As Long


Returns a ticks value which is the result of adding the specified time spans to the given ticks value.
Pass negative values if you want to subtract the values.
Example:
B4X:
Dim Tomorrow As Long 
Tomorrow = DateTime.Add(DateTime.Now, 0, 0, 1) 
Log("Tomorrow date is: " & DateTime.Date(Tomorrow))
B4X:
Sub btnTest_Click

DayOfMonth = DateTime.GetDayOfMonth(DateTime.Now)   'Get the day of the month
FirstDayOfMonth = DateTime.Add(DateTime.Now, 0, 0,  (DayOfMonth * -1) + 1)   ' change the day to 1
FirstDayOnNextMonth = DateTime.Add(FirstDayOfMonth, 0, 1, 0)  ' add 1 to the month
LastDayOfMonth = DateTime.Add(FirstDayOnNextMonth, 0, 0, -1)    ' subtract 1 from the day

ToastMessageShow("LastDayOfMonth date is: " & DateTime.Date(LastDayOfMonth),False) ' display

End Sub
About as easy to follow as I can get other than:
B4X:
LastDayOfMonth = DateTime.Add(FirstDayOfMonth, 0, 1, -1)
B4X:
Sub btnTest_Click

DayOfMonth = DateTime.GetDayOfMonth(DateTime.Now)   'Get the day of the month
FirstDayOfMonth = DateTime.Add(DateTime.Now, 0, 0,  (DayOfMonth * -1) + 1)   ' change the day to 1
LastDayOfMonth = DateTime.Add(FirstDayOfMonth, 0, 1, -1)  ' Add one month AND subtract 1 day

ToastMessageShow("LastDayOfMonth date is: " & DateTime.Date(LastDayOfMonth),true) ' display

DateTime.DateFormat= "yyyy MM dd"

ToastMessageShow("LastDayOfMonth date is: " & DateTime.Date(LastDayOfMonth),true) ' true=3sec , false=2 sec

End Sub
So 3 lines of script?
And very easy to read?
 

Attachments

  • LastDayOfMonth.zip
    5.7 KB · Views: 315
Last edited:
Upvote 0
Top