Android Question How do you know the hours and minutes that are within a starting and a final hour?

Espinosa4

Active Member
Licensed User
Longtime User
Hi everyone!

I need to know how many hours and minutes are "inside" an initial hour and a final one.

For example I started to work at 04:45 between 04:00 and 05:59 are considering night hours.
How many hours and minutes are between with those hours?

Any function?

thank you so much in advance
 

Daestrum

Expert
Licensed User
Longtime User
I think they want to know how long was spent in the period 04:00 to 05.59 - starting at 04.45 would give 1hr 14min in that period.
 
Upvote 0

Espinosa4

Active Member
Licensed User
Longtime User
I think they want to know how long was spent in the period 04:00 to 05.59 - starting at 04.45 would give 1hr 14min in that period.
Yes
That’s the problem. I know the period between but I can’t use it for this and I can’t imagine how resolve this
 
Upvote 0

Grinaute

Member
Licensed User
Longtime User
Hi Espinosa4,
Try:
B4X:
    Dim startTime As String
    Dim endTime As String
    Dim duration As Long

    startTime = "22:39" '"04:45"
    endTime = "05:59"
    
    Dim startParts() As String = Regex.Split(":", startTime)
    Dim endParts() As String = Regex.Split(":", endTime)
    
    Dim startMinutes As Long = (startParts(0) * 60) + startParts(1)
    Dim endMinutes As Long = (endParts(0) * 60) + endParts(1)

    If startMinutes > endMinutes Then
        endMinutes = endMinutes + 24 * 60
    End If

    duration = endMinutes - startMinutes
    Log("Duration: " & Round((duration / 60)) & " hours and " & (duration Mod 60) & " minutes")
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
    Dim starttime As Long = DateTime.TimeParse("04:47:00")
    Dim beginperiod As Long = DateTime.TimeParse("04:00:00")
    Dim endperiod As Long = DateTime.TimeParse("05:59:00")
    Dim elapsedtime As Long = 0
    Dim hr,mn As Int
    If starttime >= beginperiod And starttime <= endperiod Then
        elapsedtime = endperiod - starttime
        hr = (elapsedtime/3600000)
        mn = (elapsedtime Mod 3600000)/60000
        Log($"Elapsed time inside period = ${hr}:${mn}"$)
    Else
        Log("Starttime outside of specified period")    
    End If
 
Upvote 0

Espinosa4

Active Member
Licensed User
Longtime User
Thank you very much indeed! I appreciate it!

I try your codes and I’ll comment the results!

Thank you!!!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another version, based on DateUtils.
You can easily account fo an allowed range spanning multiple days.

B4X:
Sub Button1_Click
    DateTime.TimeFormat ="HH:mm"
    Dim today As String = DateTime.Date(DateTime.Now)
    'set yesterday date if shift begins before midnight
    Dim p1 As Period
    p1.Initialize
    p1.Days = -1
    Dim yesterday As String = DateTime.Date(DateUtils.AddPeriod(DateTime.Now, p1))
    Dim RangeStart As Long = DateTime.DateTimeParse(today, "04:00")        'yesterday, "22:15"
    Dim RangeStop As Long = DateTime.DateTimeParse(today, "05:59")
    Dim ShiftStart As Long = DateTime.DateTimeParse(today, "04:45")
    If (ShiftStart>=RangeStart) And (ShiftStart <= RangeStop) Then
        Dim p As Period = DateUtils.PeriodBetweenInDays(ShiftStart,RangeStop)
        Log($"Days: ${p.Days} - Hours: ${p.Hours} - Minutes:${p.Minutes}"$)
    Else
        Log("Out of allowed timeshift range")
    End If
End Sub
 
Last edited:
Upvote 1

Espinosa4

Active Member
Licensed User
Longtime User
Hi!

Thank you so much for your help!
Your codes work super and with a small change (I need to compare a couple of time frames).

Once again thank you so much for helping me.

Cheers
 
Upvote 0
Top