Android Question Comparing between tow Times ?

Alhootti

Member
I'm using the following code but not working properly What is the solution?
how i can get off this issue?

Comparing Times:
DateTime.DateFormat = "hh:mm"
    Label1.Text= DateTime.Time(DateTime.Now)
    label2.Text= "02:30"
    If Label1.Text >= label2.Text  Then
        Label3.Text = "The time is pased"
    End If
 

emexes

Expert
Licensed User
I'm using the following code but not working properly What is the solution?
how i can get off this issue?

Comparing Times:
Label1.Text= DateTime.Time(DateTime.Now)

Unix DateTimes are the number of seconds since the start of 01/01/1970 (UTC = GMT)

Android/Java DateTimes are the number of milliseconds since the start of 01/01/1970 (UTC = GMT) ie Unix DateTime x 1000

If you have two (Android) DateTimes, then you can compare them, to see which DateTime is before the other, or are they the same.

Bonus - you can also subtract them, to find out the time difference between the two.

Which is often useful for timing how long your program takes to do something, eg:

B4X:
Dim StartTime As Long = DateTime.Now
Dim BigFile As String = File.ReadString("C:\TEMP", "ENCYCOPAEDIA.TXT")
Dim EndTime As Long = DateTime.Now

Dim ElapsedTime As Long = EndTime - StartTime    'how many milliseconds did that take?

Log("Read " & BigFile.Length & " characters in " & ElapsedTime & " milliseconds")
 
Upvote 0

Alhootti

Member
Not working properly

B4X:
DateTime.TimeFormat = "hh:mm"
    Dim StartTime As Long= DateTime.Time(DateTime.Now)
    Dim EndTime As Long= DateTime.Time(DateTime.Now)
    Dim ElapsedTime As Long= EndTime - StartTime    'how many milliseconds did that take?
    Log(ElapsedTime)

Error:
Logger connected to: samsung SM-A725F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
11/15/2022
12:48:36
** Activity (main) Resume **
Error occurred on line: 44 (Main)
java.lang.NumberFormatException: For input string: "10:20"
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
11/15/2022
12:50:13
** Activity (main) Resume **
Error occurred on line: 44 (Main)
java.lang.NumberFormatException: For input string: "10:20"
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
These are wrong !
B4X:
    Dim StartTime As Long= DateTime.Time(DateTime.Now)
    Dim EndTime As Long= DateTime.Time(DateTime.Now)
DateTime.Time(DateTime.Now) returns a String not a Lonh !

They should be :
B4X:
    Dim StartTime As Long = DateTime.Now
    Dim EndTime As Long = DateTime.Now
 
Upvote 0

emexes

Expert
Licensed User
Difference of two DateTimes:
DateTime.TimeFormat = "hh:mm"

Dim StartTime As Long = DateTime.Now
Sleep(123)
Dim EndTime As Long = DateTime.Now

Dim ElapsedTime As Long = EndTime - StartTime    'how many milliseconds did that take?

Log(StartTime)
Log(EndTime)
Log("That little sleep was for " & ElapsedTime & " milliseconds")

Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
1668517536344
1668517536495
That little sleep was for 151 milliseconds
 
Upvote 0

emexes

Expert
Licensed User
The main point is knowing if Time1 is between Time2 and Time3 or not

Something like this, maybe, where we store times as decimal numbers HHMM ie hour * 100 + minute (you can't do that! release the hounds!):

B4X:
Dim Time2 As Int = "0900"    '24 hour time HHMM
Dim Time3 As Int = "1700"    'workin' 9 to 5, what a way to make a livin'

Dim Ticks As Long = DateTime.now
Dim Time1 As Int = DateTime.GetHour(Ticks) * 100 + DateTime.GetMinute(Ticks)

Log("The current time is " & NumberFormat2(Time1, 4, 0, 0, False))

If Time1 <= Time2 Then
    Log("You is early before " & Time2)
else if Time1 => Time3 Then
    Log("You is gone by " & Time3)
Else
    Log("You is here between " & Time2 & " and " & Time3)
End If

Log output (when run at 12:40am) :
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
The current time is 0040
You is early before 900
** Activity (main) Resume **
 
Upvote 0

emexes

Expert
Licensed User
I'm using the following code but not working properly What is the solution?
how i can get off this issue?

Comparing Times:
DateTime.DateFormat = "hh:mm"
    Label1.Text= DateTime.Time(DateTime.Now)
    label2.Text= "02:30"
    If Label1.Text >= label2.Text  Then
        Label3.Text = "The time is pased"
    End If

Actually, you were close here; sadly, string comparisons are done using a function rather than the <=> operators.

1668520397356.png


I think this one change would have got you over the line:

B4X:
DateTime.DateFormat = "hh:mm"
Label1.Text= DateTime.Time(DateTime.Now)

label2.Text= "02:30"

'''If Label1.Text >= label2.Text Then
If Label1.Text.CompareTo(label2.Text) >= 0  Then    'works as long as both strings are *exactly* same format eg "HH:MM" with *exactly* 4 digits
    Label3.Text = "The time is pased"
End If
 
Upvote 0

emexes

Expert
Licensed User
I'm using the following code but not working properly What is the solution?
how i can get off this issue?

Comparing Times:
DateTime.DateFormat = "hh:mm"
    Label1.Text= DateTime.Time(DateTime.Now)
    label2.Text= "02:30"
    If Label1.Text >= label2.Text  Then
        Label3.Text = "The time is pased"
    End If

Lol this would probably work too, but even if it does, the programming purists will still be at us: you can't do that! release the hounds!

B4X:
DateTime.DateFormat = "hh:mm"
Label1.Text= DateTime.Time(DateTime.Now)

label2.Text= "02:30"

'''If Label1.Text >= label2.Text Then
If Label1.Text.Replace(":", ".") >= label2.Text.Replace(":", ".") Then    'convert/cast to numbers so can use numeric comparison operators
    Label3.Text = "The time is pased"
End If

I think even .Replace(":", "") ie removing the colon altogether rather than replacing it with a decimal point, might work too.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
To compare 2 times, you have to involve the date also in case the times are in different dates. Use your own dateformat
B4X:
DateTime.DateFormat= "MM/dd/yyyy hh:mm a" 'you have to involve the date also
    Log(IsTimeBetween( "11/12/2022 6:45 AM", "11/12/2022 3:55 AM")) 'false
    Log(IsTimeBetween( "11/12/2022 6:45 AM", "11/12/2022 11:55 AM")) 'true
B4X:
Sub IsTimeBetween( StartTime  As String, EndTime  As String) As Boolean
    Return (DateTime.DateParse(EndTime) >= DateTime.DateParse(StartTime))
End Sub
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Try this

B4X:
For Each Time As String In Array As String( "01:00", "02:00", "02:29", "02:30", "02:50", "12:30", "22:30", "23:00")
    Log( $"${Time} >= 02:30  ${TimePassed( Time, "02:30")}"$)
Next

'Input Format: HH:MM
Private Sub TimePassed( TimeString1 As String, TimeString2 As String) As Boolean
    DateTime.TimeFormat = "HH:mm"
    Return (DateTime.TimeParse( TimeString1) >= DateTime.TimeParse( TimeString2))
End Sub
01:00 >= 02:30 false
02:00 >= 02:30 false
02:29 >= 02:30 false
02:30 >= 02:30 true
02:50 >= 02:30 true
12:30 >= 02:30 true
22:30 >= 02:30 true
23:00 >= 02:30 true
 
Upvote 0
Top