Android Question Days elapsed in a month

Brian Michael

Active Member
Licensed User
Hi B4X Dev's:

Hi i make another sub for get the day elapsed in the month


B4X:
Public Sub CurrentLaborableDays(Date As Long, Mode As Int) As Int
    
    Dim days_count As Int = DateTime.GetDayOfMonth(Today)
    
    Dim AD As Avalibles_Days
    AD.Initialize
    
    Select Mode
        Case 1 'DAYS WITHOUT SATURDAY AND SUNDAY
            AD.Monday = True
            AD.Tuesday = True
            AD.Wednesday = True
            AD.Thursday = True
            AD.Friday = True
            AD.Saturday = True
            AD.Sunday = False           
        Case 2 'DAYS WITHOUT SUNDAY
            AD.Monday =True
            AD.Tuesday =True
            AD.Wednesday =True
            AD.Thursday =True
            AD.Friday =True
            AD.Saturday =False
            AD.Sunday =False
        Case Else 'ALL DAYS
            AD.Monday =True
            AD.Tuesday =True
            AD.Wednesday =True
            AD.Thursday =True
            AD.Friday =True
            AD.Saturday =True
            AD.Sunday =True
    End Select
    
    Dim days_result As Int = 0

    For i = 1 To days_count
        Dim day As String =  i & "/" & DateTime.GetMonth(Date) & "/" & DateTime.GetYear(Date)
        'My language is spanish that's the reason the days are in spanish, but you can change it for your language:

        Dim l As Long = DateTime.DateParse(day)
        day = DateUtils.GetDayOfWeekName(l)
        If day = "lunes" And AD.Monday = True Then days_result = days_result + 1
        If day = "martes" And AD.Tuesday = True Then days_result = days_result + 1
        If day = "miércoles" And AD.Wednesday = True Then days_result = days_result + 1
        If day = "jueves" And AD.Thursday = True Then days_result = days_result + 1
        If day = "viernes" And AD.Friday = True Then days_result = days_result + 1
        If day = "sábado" And AD.Saturday = True Then days_result = days_result + 1
        If day = "domingo" And AD.Sunday = True Then days_result = days_result + 1
    Next
    
    Return days_result
End Sub


How to use the sub:

B4X:
CurrentLaborableDays(Date as Long, Mode as Int) as Int
'Date is the current day
'Example: Let's say the month is December, if you put the "Current day" = 12/8/2020 when you run the function in
'Mode 0 = 8, Mode 1 = 7, Mode 2 = 6

'Mode 0 = All Days
'Mode 1 = Days With out Sunday
'Mode 2 =  Days With out Saturday and Sunday


Log("Days: " & CurrentLaborableDays(DateTime.DateParse("12/8/2020"),0))
'Result 8

Log("Days: " & CurrentLaborableDays(DateTime.DateParse("12/8/2020"),1))
'Result 7

Log("Days: " & CurrentLaborableDays(DateTime.DateParse("12/8/2020"),2))
'Result 6
 

DonManfred

Expert
Licensed User
Longtime User
Select Mode Case 1 'DAYS WITHOUT SATURDAY AND SUNDAY AD.Monday = True AD.Tuesday = True AD.Wednesday = True AD.Thursday = True AD.Friday = True AD.Saturday = True AD.Sunday = False
must AD.Sunday not be true for case 1?
Case 2 'DAYS WITHOUT SUNDAY

Days without sunday implies Saturday for me.

You are missing to post the needed Custom type.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Depends on DateUtils and B4XCollections:
B4X:
Sub AppStart (Args() As String)
    Dim InterestingDays As B4XSet = B4XCollections.CreateSet2(Array(1, 2, 3, 4, 5)) 'without friday and saturday
    Log(CountSpecificDaysInMonth(2020, 12, InterestingDays))
End Sub

Sub CountSpecificDaysInMonth (Year As Int, Month As Int, RelevantDays As B4XSet) As Int
    Dim day As Long = DateUtils.SetDate(Year, Month, 1)
    Dim p As Period
    p.Days = 1
    Dim total As Int
    Do While DateTime.GetMonth(day) = Month
        If RelevantDays.Contains(DateTime.GetDayOfWeek(day)) Then total = total + 1
        day = DateUtils.AddPeriod(day, p)
    Loop
    Return total
End Sub
 
Upvote 0

Brian Michael

Active Member
Licensed User
Hi @DonManfred and @Erel

Thanks for answer, this function can be added to DateUtils:

Depends on DateUtils and B4XCollections:
B4X:
Sub AppStart (Args() As String)
    Dim InterestingDays As B4XSet = B4XCollections.CreateSet2(Array(1, 2, 3, 4, 5)) 'without friday and saturday
    Log(CountSpecificDaysInMonth(2020, 12, InterestingDays))
End Sub

Sub CountSpecificDaysInMonth (Year As Int, Month As Int, RelevantDays As B4XSet) As Int
    Dim day As Long = DateUtils.SetDate(Year, Month, 1)
    Dim p As Period
    p.Days = 1
    Dim total As Int
    Do While DateTime.GetMonth(day) = Month
        If RelevantDays.Contains(DateTime.GetDayOfWeek(day)) Then total = total + 1
        day = DateUtils.AddPeriod(day, p)
    Loop
    Return total
End Sub
 
Upvote 0
Top