# Android QuestionDifference between PM time today and AM time of tomorrow

#### jkhazraji

##### Active Member
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
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.

#### Mahares

##### Expert
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``````

#### jkhazraji

##### Active Member
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 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``````

#### Mahares

##### Expert
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

• MikeSW17

#### jkhazraji

##### Active Member
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``````
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 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:
• AnandGupta

#### Erel

##### B4X founder
Staff member
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``````

• AnandGupta and jkhazraji

#### jkhazraji

##### Active Member
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

Replies
714
Views
41K
Android Question DateUtils - PeriodBetween
Replies
3
Views
2K
Replies
22
Views
16K
Replies
7
Views
1K
Replies
40
Views
6K