listview DateTime from sql

sigster

Active Member
Licensed User
Longtime User
Hi , need help with this Please

I am try to get open time I have two field
what I am try to do is if mobile time is before open and after close
then I write in the listview CLOSE

open ,, when the place is open in the morning
close ,, when the place is close in the night

B4X:
    Cursor = SQL.ExecQuery("select * from plases where nafn like '" & tt.nafn & "%' order by nafn;")
         
     ' Listview
      For i = 0 To Cursor.RowCount - 1
    Dim tt As personangsmnn
         Cursor.Position = i
    tt.nafn=Cursor.GetString("weekday")
    tt.opidfra=Cursor.GetString("open")
    tt.opidtil=Cursor.GetString("close")
    
   DateTime.TimeFormat="HH:mm"
   If DateTime.TimeParse(DateTime.Time(DateTime.Now)) < DateTime.TimeParse(tt.opidfra) AND DateTime.TimeParse(DateTime.Time(DateTime.Now)) > DateTime.TimeParse(tt.opidtil) Then
        Listview_info.AddTwoLines2 (tt.nafn & " CLOSE ",tt.opidfra & " = " & tt.opidtil,tt)
    Else
        Listview_info.AddTwoLines2 (tt.nafn,tt.opidfra & " = " & tt.opidtil,tt)
    End If
    Next
    Cursor.Close
 

DouglasNYoung

Active Member
Licensed User
Longtime User
Hi sigster,
No one else seems to be picking this up so its over to me!

Since your times are held as strings in the database, the following should work

B4X:
If DateTime.Time(DateTime.Now) < t.opidfra AND DateTime.Time(DateTime.Now) > tt.opidtil Then

Douglas
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
Thanks DouglasNYoung

I get this error
NumberFormatException

B4X:
If DateTime.Time(DateTime.Now) < t.opidfra AND DateTime.Time(DateTime.Now) > tt.opidtil Then
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
This works , Thanks for your help

B4X:
   If DateTime.TimeParse(DateTime.Time(DateTime.Now)) > DateTime.TimeParse(tt.opidfra) AND DateTime.TimeParse(DateTime.Time(DateTime.Now)) < DateTime.TimeParse(tt.opidtil) Then

Regards
Sigster
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Sorry 'sigster' I didn't look at the entire code, just spotted one thing.

The error you report is the comparing strings with the mathematical operators '<' and '>'. In B4A you should use the string.compare() function. As I dont have your database and only a snippet of code I have simulated things to get a working example; similarly I have shown the results of the 'if' statement with ToastMessages - Hopefully this will show the way forward!

B4X:
Dim timenow As String
Dim nafn, opidfra, opidtil As String

DateTime.TimeFormat="HH:mm"
timenow = DateTime.Time(DateTime.Now)

nafn = "nafn string"  ' tt.nafn=Cursor.GetString("weekday")
opidfra = "09:00"    '  tt.opidfra=Cursor.GetString("open")
opidtil = "22:00"    '  tt.opidtil=Cursor.GetString("close")

Log( timenow & " ; " & opidfra & " ; " & opidtil )

If timenow.CompareTo(opidfra) < 0  OR timenow.CompareTo(opidtil) > 0 Then
     ToastMessageShow(nafn & " CLOSED : Open between " & opidfra & " and " & opidtil,True)
Else
   ToastMessageShow(nafn & " OPEN : " & opidfra & " to " & opidtil, True)
End If

Also I think you may have a logic error in the AND - should it perhaps be an OR ?

Douglas
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
Thanks DouglasNYoung

this is my code and it work well !



B4X:
   ProgressDialogShow("open hours")
     Listview_sundstadir.Clear   
  
   Dim timenow As String
   DateTime.TimeFormat="HH:mm"
   timenow = DateTime.Time(DateTime.Now)

    Cursor = SQL.ExecQuery("select * from sundstadirdagar where vikudagur like '" & Value & "%' order by nafn;")

    For i = 0 To Cursor.RowCount - 1
    Dim tt As personangsmnn
     Cursor.Position = i
     tt.nafn=Cursor.GetString("nafn")
     tt.opidfra=Cursor.GetString("opid_fra")
     tt.opidtil=Cursor.GetString("opid_til")
    
    If timenow.CompareTo(tt.opidfra) < 0  OR timenow.CompareTo(tt.opidtil) > 0 Then
       Listview_sundstadir.AddTwoLines2 (tt.nafn,  " CLOSED : Open between " & tt.opidfra & " and " & tt.opidtil,tt)
    Else
    Listview_sundstadir.AddTwoLines2 (tt.nafn, " OPEN " & tt.opidfra & " to " & tt.opidtil,tt)

    End If
     Next
    Cursor.Close
   
    ProgressDialogHide
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…