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)

Waiting for debugger to connect...
Program started.
Error: Invalid value: AD20170631 175800
Error occurred on line: 40 (Main)
java.lang.NumberFormatException: For input string: "invalid date"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at b4j.example.dateutils._setdateandtime(dateutils.java:433)
at b4j.example.main._appstart(main.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.start(main.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

Line 40 is Dim t1 As Long...

Any ideas on what I am doing wrong ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
For input string: "invalid date"

Invalid value: AD20170631 175800

Sounds like the problem is not in the code you posted as you are not using any strings here
 
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
Top