Android Code Snippet [B4X] Elapsed Days between 2 dates with Specific Days in Month

Hi everyone, based on @Erel example HERE i edit the code for get what title says.


ElapsedDaysInMonth:
Public Sub ElapsedDaysInMonth (FirstDay As Long, LastDay As Long, RelevantDays As B4XSet) As Int
    
    Dim Day As Long = DateUtils.SetDate(DateTime.GetYear(FirstDay), DateTime.GetMonth(FirstDay), DateTime.GetDayOfMonth(FirstDay))
    Dim p As Period : p.Days = 1
    Dim total As Int
    Do While DateTime.GetMonth(Day) = DateTime.GetMonth(LastDay)
        If RelevantDays.Contains(DateTime.GetDayOfWeek(Day)) Then total = total + 1
        Day = DateUtils.AddPeriod(Day, p)
    Loop
    Return total
End Sub

Example::
Dim ID As B4XSet = B4XCollections.CreateSet2(Array(1, 2, 3, 4, 5)) 'number of days excluding Friday and Saturday
Dim elapDays As Int = ElapsedDaysInMonth(Date1,Date2,ID)
 

aeric

Expert
Licensed User
Longtime User
I think this line:
B4X:
Dim Day As Long = DateUtils.SetDate(DateTime.GetYear(FirstDay), DateTime.GetMonth(FirstDay), DateTime.GetDayOfMonth(FirstDay))
Can be simply write as:
B4X:
Dim Day As Long = FirstDay

Also maybe can make it more flexible if it can return days between 2 dates that belong to different month and year.
 

Brian Michael

Active Member
Licensed User
I think this line:
B4X:
Dim Day As Long = DateUtils.SetDate(DateTime.GetYear(FirstDay), DateTime.GetMonth(FirstDay), DateTime.GetDayOfMonth(FirstDay))
Can be simply write as:
B4X:
Dim Day As Long = FirstDay

Also maybe can make it more flexible if it can return days between 2 dates that belong to different month and year.


Thanks @aeric.

I tried like that but for a mysterious reason didn't work. 😮💨
Also you can share here your alternatives or modify the code for more useful code.🫡
 

aeric

Expert
Licensed User
Longtime User
I am not sure how you determine the day elapsed.
I tried with FirstDay = 23/06 and LastDay = 29/06 and I get 6 days.
B4X:
Do While DateTime.GetMonth(Day) = DateTime.GetMonth(LastDay)
I think it should return 5 days.
I modify my code as:
B4X:
Do While Day < LastDay 'DateTime.GetMonth(Day) = DateTime.GetMonth(LastDay)
 

aeric

Expert
Licensed User
Longtime User
Here is my final code:
B4X:
Public Sub ElapsedDaysBetween2Dates (FirstDay As Long, LastDay As Long, RelevantDays As B4XSet) As Int
    Dim Day As Long = FirstDay
    Dim p As Period : p.Days = 1
    Dim total As Int
    Do While Day < LastDay
        If RelevantDays.Contains(DateTime.GetDayOfWeek(Day)) Then total = total + 1
        Day = DateUtils.AddPeriod(Day, p)
    Loop
    Return total
End Sub

B4X:
Dim Date1 As Long = DateUtils.SetDate(2024, 5, 1)
Dim Date2 As Long = DateUtils.SetDate(2024, 6, 30)
Dim ID As B4XSet = B4XCollections.CreateSet2(Array(2, 3, 4, 5, 6)) ' Number of days excluding Sunday and Saturday
Dim WorkingDays As Int = ElapsedDaysBetween2Dates(Date1, Date2, ID)
DateTime.DateFormat = "dd/MM/yyyy"
Log($"My total working days between ${DateTime.Date(Date1)} and ${DateTime.Date(Date2)} is ${WorkingDays} days."$)
 
Top