Android Question Laborable Days in Month

Brian Michael

Active Member
Licensed User
Hi B4X Dev's,

I make this code for get working days in month, you can get all days, days without saturday or days without saturday and sunday.


You can make it more simple if you can:

B4X:
Sub Process_Globals
    Type Avalibles_Days(Monday As Boolean, Tuesday As Boolean, _
    Wednesday As Boolean,Thursday As Boolean,Friday As Boolean, _
    Saturday As Boolean,Sunday As Boolean)
End Sub

B4X:
Public Sub laborableDays(Month As Int, Year As Int, Mode As Int) As Int
    Dim days_count As Int = DateUtils.NumberOfDaysInMonth(Month,Year)
   
    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 & "/" & Month & "/" & Year

        Dim l As Long = DateTime.DateParse(day)
        day = DateUtils.GetDayOfWeekName(l)
       'My language is spanish that's the reason the days are in spanish, but you can change it for your language:

        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

For run it its very simple:

B4X:
laborableDays(Month As Int, Year As Int, Mode As Int)
'Mode 0 = All Days
'Mode 1 = Days With out Sunday
'Mode 2 =  Days With out Saturday and Sunday


Log("Laborable days :" & laborableDays(12, 2020, 0))
'Return 31

Log("Laborable days :" & laborableDays(12, 2020, 1))
'Return 27

Log("Laborable days :" & laborableDays(12, 2020, 2))
'Return 23
 
Last edited:

Brian Michael

Active Member
Licensed User
Thanks @Erel for answer, i just try to solve a need, maybe you can add a function to get this data on DateUtils

Your code will fail on most devices as the days names is based on the device locale.

You should use DateTime.GetDayOfWeek and compare the number.

Moved to the questions forum as there are many better ways to do it.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thanks @Erel for answer, i just try to solve a need, maybe you can add a function to get this data on DateUtils

Good job Brian. Erel is right... You can upgrade your code and make it cross language because the Datetime.GetDayOfWeek returns a number (1-7) with Sunday being 1.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Brian Michael

Active Member
Licensed User
Good job Brian. Erel is right... You can upgrade your code and make it cross language because the Datetime.GetDayOfWeek returns a number (1-7) with Sunday being 1.

I really appreciate your message, thank you, it motivates me to keep learning.
I will update the code to solve that on Sundays
 
Upvote 0
Top