Android Question [Solved]How to get Date and Time difference between two dateTimes

AndroidMadhu

Active Member
Licensed User
Hello,
I want to calculate or how can I get a date difference[in Hours and Minutes] between two dates with times.
Any example will be very helpful.

Thanks
 

udg

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Num3

Active Member
Licensed User
Longtime User
Use DateUtils like udg mentioned.

Hope this helps:
B4X:
    Dim timeleft As Period = DateUtils.PeriodBetween( DateTime.Now,DateTime.Now + DateTime.TicksPerHour * 2.3)
    Log($"$2{timeleft.hours}:$2{timeleft.Minutes}:$2{timeleft.Seconds}"$ )
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
get a date difference[in Hours and Minutes] between two dates with times.
Here is how I would have done it to comply with your question details:
B4X:
Sub TimeDiffHoursMinutes(ds As String, de As String) As String
    Dim p As Period
    DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
    p=DateUtils.PeriodBetweenindays(DateTime.DateParse(ds), DateTime.DateParse(de))
    Return $"${(p.Days)*24 +p.hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$    
End Sub
To test ( if no need for seconds, remove them from code):
B4X:
Log(TimeDiffHoursMinutes("2021-02-18 17:30:00", "2021-02-18 19:48:00"))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sorry to say but only my answer is 100% correct.

Post #4 is wrong as it ignores the days and months.
Post #5 is mostly correct but it assumes that there are 24 hours per day. There are also days with 23 hours or 25 hours.
(it is also better to accept ticks instead of string)

If you are interested in difference measured in hours and minutes then you shouldn't use DateUtils.PeriodBetween. This one solves a more complicated problem.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
only my answer
C'mon, post #2 is as much correct as yours.. :)
Ok, it was a bit too succinct and it's based on the same assumption of dates+times as in your first sentence, but it earned some merit too.
And , for once, I was quicker than you :D

ps: obviously joking..and almost ready for some jogging..hehehe
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
C'mon, post #2 is as much correct as yours..
No. DateUtils isn't needed here.

B4X:
Sub AppStart (Args() As String)
    Log(HoursMinutesAndSecondsBetweenToTicks(DateTime.Now, DateTime.DateParse("02/23/2021")))
End Sub

Sub HoursMinutesAndSecondsBetweenToTicks (StartTime As Long, EndTime As Long) As String
    Return ConvertMillisecondsToString(EndTime - StartTime)
End Sub

Sub ConvertMillisecondsToString (t As Long) As String
    Dim Hours, minutes, seconds As Int
    Hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return $"$1.0{Hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
How many coffees did you sip at this time today? I had to re-read the first post a few times before absorbing this: date difference[in Hours and Minutes] !
Anyway, maybe a short note to your example code could be useful: DateUtils.DateParse will set the time to 00:00:00 so the code "as is" computes the difference between Now (date and current time) to "new date" at midnight.

Eventually a similar note could be added to the help window when hovering on that function.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Once you had your cup of coffee (better two cups), you may want to read this article. It's just one out of several ones you may find on the Internet.
This one let you compute day duration for any date.
 
Upvote 0

Num3

Active Member
Licensed User
Longtime User
It wasn't a joke. These are the days where the time zone offset changes because of the daylight saving (DST).
I know, my example was based on my own needs (countdown timer with max 1 hour period).
But indeed your code is more reliable, it takes into consideration DST and the user changing time zone!
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
It wasn't a joke. These are the days where the time zone offset changes because of the daylight saving (DST).
And if you are interested in minutes and seconds over a long period, you need to factor in leap seconds...

Be careful, precision timing is a rabbit hole! (speaking for a friend...)
 
Upvote 0
Top