Android Question Difference between PM time today and AM time of tomorrow

jkhazraji

Active Member
Licensed User
Longtime User
I have been trying to calculate the difference between a PM clock time of a certain day e.g. 9:00pm which is (21:00) and an AM clock time of the next day e.g., 4:23am (4:23) .. to get the result in ticks or periods (hours and minutes) but all of my efforts were unsuccessful, like adding a day to the next time, to no avail.. Any help will be greatly appreciated..
 

MikeSW17

Active Member
Licensed User
Longtime User
Should be pretty simple. e.g.
Convert first time to minutes until Midnight. [ = 1440 - ((Hours*60)+Minutes) ]
Convert second time to minutes after Midnight [ = (Hours*60) + Minutes ) ]
Add first and second to get total minutes between times. 'TimeDiff'
Convert minutes between back to Hours and minutes. [ Hours = Int(TimeDiff/60); Mins = TimeDiff MOD 60.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
help will be greatly appreciated..
The correct way to do it is with DateUtils.PeriodBetweenindays . You need to put a check for DateUtils library
B4X:
DateTime.DateFormat  = "MM/dd/yyy HH:mm:ss"
Dim d1, d2 As String
d1="3/28/2021 14:30:00"
d2="3/29/2021 11:30:00"
Dim p As Period
p=DateUtils.PeriodBetweenindays(DateTime.DateParse(d1),DateTime.DateParse(d2))
Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$)  '0 days 21 hours 0 min 0 sec

B4X:
DateTime.DateFormat  = "MM/dd/yyy HH:mm:ss"
Dim d1, d2 As String
d1="3/28/2021 21:00:00"
d2="3/29/2021 04:23:00"
Dim p As Period
p=DateUtils.PeriodBetweenindays(DateTime.DateParse(d1),DateTime.DateParse(d2))
Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$)  '0 days 7 hours 23 min 0 sec
You can change your date time format to your preference.
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
I have tried the following on b4i:
‘B4x code:
DateTime.timeformat = “HH:mm”
dim tick1 as long = datetime.parsedatetime(datetime.date(datetime.now),datetime.time(datetime.now))
dim tick2 as long = datetime.parsedatetime(datetime.date(datetime.now),"11:59")
Dim aday as Period
aday.Days = 1
dim tick3 as long = datetime.parsedatetime(DateUtils.AddPeriod(datetime.date(datetime.now),aday),"4:35")

dim diff1 as long = tick2 - tick1
dim diff2 as long = tick3 - tick2
dim diff as long = diff1 + diff2

dim hours as int  = diff / DateTime.ticksperhour
dim minutes as int = diff / DateTime.ticksperminute

'*but it did not work
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
but it did not work
You are not taking the right approach to solve this. What are the two dates with their time you are trying to find the difference between. What is your date time format. Is it DateTime.DateFormat = "MM/dd/yyy HH:mm:ss" or "yyyy-MM-dd hh:mm a", or whatever. You need that format
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
The correct way to do it is with DateUtils.PeriodBetweenindays . You need to put a check for DateUtils library
B4X:
DateTime.DateFormat  = "MM/dd/yyy HH:mm:ss"
Dim d1, d2 As String
d1="3/28/2021 14:30:00"
d2="3/29/2021 11:30:00"
Dim p As Period
p=DateUtils.PeriodBetweenindays(DateTime.DateParse(d1),DateTime.DateParse(d2))
Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$)  '0 days 21 hours 0 min 0 sec

B4X:
DateTime.DateFormat  = "MM/dd/yyy HH:mm:ss"
Dim d1, d2 As String
d1="3/28/2021 21:00:00"
d2="3/29/2021 04:23:00"
Dim p As Period
p=DateUtils.PeriodBetweenindays(DateTime.DateParse(d1),DateTime.DateParse(d2))
Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$)  '0 days 7 hours 23 min 0 sec
You can change your date time format to your preference.
Great approach!! Thank you very much. That worked.
I even made your solution a sub that I hope it will be of use to others. cheers!
Period to any time in the next days:
'timeToday is the start time.
'timeAfter is the end time.
'numDays the number of days in between.
Private Sub TimeToLaterTime(timeToday As String, timeAfter As String, numDays As Int) As String
   
   
    DateTime.DateFormat  = "MM/dd/yyy HH:mm:ss"
    Dim d1, d2 As String
    d1 =DateTime.Date( DateTime.Now)
    Dim substr() As String = Regex.Split(" ",d1)
    d1 = substr(0) & " " & timeToday
    'Add a period equal to the number of days
    Dim aday As Period
    aday.Days = numDays
   
    d2 =DateTime.Date( DateUtils.AddPeriod(DateTime.Now,aday))
    Dim substr() As String = Regex.Split(" ",d2)
    d2 = substr(0) & " " & timeAfter

   
    Dim p As Period
    p=DateUtils.PeriodBetweenindays(DateTime.DateParse(d1) ,DateTime.DateParse(d2))
    '/Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$)  '0 days 21 hours 0 min 0 sec
    Return p.Hours & ":" p.Minutes & ":" p.Seconds
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is how I would have done it:
B4X:
Sub TimeToLaterTime (HoursToday As Int, MinutesToday As Int, _
        HoursAfter As Int, MinutesAfter As Int, NumDays As Int) As Period
    Dim now As Long = DateTime.Now
    Dim TimeToday As Long = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), _
        DateTime.GetDayOfMonth(now), HoursToday, MinutesToday, 0)
    Dim p As Period
    p.Days = NumDays
    Dim After As Long = DateUtils.AddPeriod(now, p)
    Dim TimeAfter As Long = DateUtils.SetDateAndTime(DateTime.GetYear(After), DateTime.GetMonth(After), _
        DateTime.GetDayOfMonth(After), HoursAfter, MinutesAfter, 0)
    Return DateUtils.PeriodBetweenInDays(TimeToday, TimeAfter)
End Sub
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
This is how I would have done it:
B4X:
Sub TimeToLaterTime (HoursToday As Int, MinutesToday As Int, _
        HoursAfter As Int, MinutesAfter As Int, NumDays As Int) As Period
    Dim now As Long = DateTime.Now
    Dim TimeToday As Long = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), _
        DateTime.GetDayOfMonth(now), HoursToday, MinutesToday, 0)
    Dim p As Period
    p.Days = NumDays
    Dim After As Long = DateUtils.AddPeriod(now, p)
    Dim TimeAfter As Long = DateUtils.SetDateAndTime(DateTime.GetYear(After), DateTime.GetMonth(After), _
        DateTime.GetDayOfMonth(After), HoursAfter, MinutesAfter, 0)
    Return DateUtils.PeriodBetweenInDays(TimeToday, TimeAfter)
End Sub
Awesome.. a great addition @Erel .. It even achieves more
 
Upvote 0
Top