B4J Question Time Difference between 2 times

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to work out how to calculate the minutes between 2 times.

For Example:

Tue, 18-07 17:11 and Wed, 19-07 20:24

I did look at the following code but wasn't sure on what I need to use for D1, T1, D2, T2.

I know D1, T1 D2, T2 is the date and time, but wasn't sure on how to pass on this value based on my time/date as per my example above.

B4X:
Sub DateBetweenTwoDates(D1 As String, T1 As String, D2 As String, T2 As String)
    Dim start, endTime, t As Long
    start = DateTime.DateTimeParse(D1, T1)
    endTime = DateTime.DateTimeParse(D2, T2)
    Dim days, hours, minutes, seconds As Int
    t = Abs(endTime - start)
    days = Floor(t / DateTime.TicksPerDay)
    hours = Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
    minutes = Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
    seconds = Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
   
    Log("Days = " & days)
    Log("Hours = " & hours)
    Log("Minutes = " & minutes)
    Log("Seconds = " & seconds)
    log("Days = " & days & CRLF & "Hours = " & hours & CRLF & "Minutes = " & minutes & CRLF & "Seconds = " & seconds)
   
End Sub

Any ideas ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
You should have a look at PeriodBetween in the jDateUtils library.
When I tried I couldn't get it to work. I am guessing I am doing it wrong ?

B4X:
Dim t1 As Long = DateUtils.SetDateAndTime(2017,06,31,17,58,00) ' Time/Date1
    Dim t2 As Long = DateUtils.SetDateAndTime(2017,06,31,17,59,00) ' Time/Date2
   
    Dim p As Period = DateUtils.PeriodBetween(t1,t2)
   
    Log(p.Years)
    Log(p.Months)
    Log(p.Days)
    Log(p.Hours)
    Log(p.Minutes)
    Log(p.Seconds)


Line 40 is Dim t1 As Long...

Any ideas on what I am doing wrong ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You'll kick yourself. There is no 31 June
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
ou'll kick yourself. There is no 31 June

Yeah just noticed that.

Thirty days hath September,
April, June, and November;
All the rest have thirty-one,
Excepting February alone,
And that has twenty-eight days clear
And twenty-nine in each leap year
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…