Android Question time calculation (solved)

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi guys,
DateTime.Time(DateTime.Now is showing the time now,
How can I do something (Log("OFF")) if time is for example > 22:00 ?

it is my try :oops:
also I need to remove seconds just want to see hour and minutes
B4X:
    ButtonTimeNow.Text=DateTime.Time(DateTime.Now)
    Log(DateTime.TimeZoneOffset)
    Log(DateTime.Time(DateTime.Now))
    If DateTime.Time(DateTime.Now) > "22:18:00" Then
        Log("OFF")
    End If

thnaks
 

f0raster0

Well-Known Member
Licensed User
Longtime User
it doesn't work
B4X:
    If DateTime.GETHOUR(DateTime.Now)>22 And DateTime.GetMinute(DateTime.Now)>18 Then
        Log("OFF")
    End If
but the previous one that you wrote works (hour only)
B4X:
If DateTime.GetHour(DateTime.Now)>22 Then
        Log("OFF")
End If
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This works:
B4X:
If DateTime.GetHour(DateTime.Now) >= 22 And DateTime.GetMinute(DateTime.Now) > 18 Then
    Log("OFF")
End If

EDIT:
The code above is wrong. The correct code is, like in post#11:
B4X:
If DateTime.GetHour(DateTime.Now) = 22 And DateTime.GetMinute(DateTime.Now) > 18 Or DateTime.GetHour(DateTime.Now) > 22 Then
    Log("OFF")
End If
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Why it doesn't work? What is the result?
nothing I don't get the log Log("OFF")

Do you use the following?
B4X:
DateTime.TimeFormat = "HH:mm:ss"
I use this:
B4X:
ButtonTimeNow.Text=DateTime.Time(DateTime.Now)
    Log(DateTime.TimeZoneOffset)
    Log(DateTime.Time(DateTime.Now))
then I got the time like: HH:mm:ss
I want to something like:
If DateTime.Time(DateTime.Now) > "22:18:00" Then
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Assuming (the switch) will "OFF" after 10:18PM up to 11:59PM.
B4X:
    Dim tt As Long
    tt = DateUtils.SetDateAndTime(2020,2,11,22,18,1)
   
    If DateTime.GetHour(tt) > 22 Then
        Log("OFF")
    Else If DateTime.GetHour(tt) = 22 Then
        If DateTime.GetMinute(tt) > 18 Then
            Log("OFF")
        Else If DateTime.GetMinute(tt) = 18 Then
            If DateTime.GetSecond(tt) > 0 Then
                Log("OFF")
            Else
                Log("ON")
            End If
        Else
            Log("ON")
        End If       
    Else
        Log("ON")
    End If
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
It will, of course, not work !

This works better:
B4X:
If DateTime.GetHour(DateTime.Now) = 22 And DateTime.GetMinute(DateTime.Now) >18 Or DateTime.GetHour(DateTime.Now) > 22 Then
    Log("OFF")
End If

yes.. you always right!! thanks very much @klaus
Not allways.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
What happen when time is 23:15 ?
yes.. you are right too Jjaja doesn't work..
B4X:
If DateTime.GetHour(DateTime.Now) >= 23 And DateTime.GetMinute(DateTime.Now) > 58 Then
        Log("OFF")
End If
here it is 00:37:28 and the Log(OFF) doesn't show up :oops: when restarted the App

It will, of course, not work !

This works better:
B4X:
If DateTime.GetHour(DateTime.Now) = 22 And DateTime.GetMinute(DateTime.Now) >18 Or DateTime.GetHour(DateTime.Now) > 22 Then
    Log("OFF")
End If

here it is 00:37:28 and the Log(OFF) doesn't show up o_O


It will, of course, not work !
yes.. it looks not easy..
I think, better continue clicking the button manually to close the system to not receive takeaways orders :D
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
you could try this too:

B4X:
    Dim OffHour1 = 22, OffMinute1 = 18, OffHour2 = 7, OffMinute2 = 15  As Int
   
    Dim nowTime As Long = (DateTime.GetHour(DateTime.Now)*DateTime.TicksPerHour)+(DateTime.GetMinute(DateTime.Now)*DateTime.TicksPerMinute)
    Dim offTime1 As Long = (DateTime.TicksPerHour*OffHour1)+(DateTime.TicksPerMinute*OffMinute1)
    Dim offTime2 As Long = (DateTime.TicksPerHour*OffHour2)+(DateTime.TicksPerMinute*OffMinute2)

    If nowTime > offTime1 OR nowTime < offTime2 Then
        Log("OFF")
    Else
        Log("ON")   
    End If

EDIT: i edited the post, now OFF will be between 22:18 until 7:15 !!

EDIT2: changed AND to OR
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
you could try this too:

B4X:
    Dim OffHour = 22, OffMinute = 18 As Int
    Dim nowTime As Long = (DateTime.GetHour(DateTime.Now)*DateTime.TicksPerHour)+(DateTime.GetMinute(DateTime.Now)*DateTime.TicksPerMinute)
    Dim offTime As Long = (DateTime.TicksPerHour*OffHour)+(DateTime.TicksPerMinute*OffMinute)

    If nowTime > offTime Then
        Log("OFF")
    Else
        Log("ON")
    End If
Dim OffHour = 00, OffMinute = 18 As Int
it is working...00:59:58show the log OFF
let me wait 2 mintus :D:cool:

EDIT: it looks like is working..
Dim OffHour = 00, OffMinute = 18 As Int
01:02:50
OFF
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
thanks @ilan it works!! between 2 different times, perfect for a restaurant: 11am - 3pm & 4.45pm - 9pm ?‍??‍?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
after checking the code again there is a mistake. change the AND in the if else function to OR

this also should work and its simpler to understand:

B4X:
    Dim OffHour1 = 16, OffMinute1 = 18, OffHour2 = 7, OffMinute2 = 15  As Int
   
    Dim nowTime As Long = (DateTime.GetHour(DateTime.Now)*60)+DateTime.GetMinute(DateTime.Now)
    Dim offTime1 As Long = (OffHour1*60)+OffMinute1
    Dim offTime2 As Long = (OffHour2*60)+OffMinute2
   
    If nowTime > offTime1 Or nowTime < offTime2 Then
        Log("OFF")
    Else
        Log("ON")  
    End If
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
doesn't work for me that option
after checking the code again there is a mistake. change the AND in the if else function to OR

this also should work and its simpler to understand:

B4X:
    Dim OffHour1 = 16, OffMinute1 = 18, OffHour2 = 7, OffMinute2 = 15  As Int

    Dim nowTime As Long = (DateTime.GetHour(DateTime.Now)*60)+DateTime.GetMinute(DateTime.Now)
    Dim offTime1 As Long = (OffHour1*60)+OffMinute1
    Dim offTime2 As Long = (OffHour2*60)+OffMinute2

    If nowTime > offTime1 Or nowTime < offTime2 Then
        Log("OFF")
    Else
        Log("ON")
    End If


but your previous code just work perfect:
B4X:
    Dim OffHour1 = 19, OffMinute1 = 19, OffHour2 = 19, OffMinute2 = 21  As Int
  
    Dim nowTime As Long = (DateTime.GetHour(DateTime.Now)*DateTime.TicksPerHour)+(DateTime.GetMinute(DateTime.Now)*DateTime.TicksPerMinute)
    Dim offTime1 As Long = (DateTime.TicksPerHour*OffHour1)+(DateTime.TicksPerMinute*OffMinute1)
    Dim offTime2 As Long = (DateTime.TicksPerHour*OffHour2)+(DateTime.TicksPerMinute*OffMinute2)

    If nowTime > offTime1 And nowTime < offTime2 Then
        Log(DateTime.Time(DateTime.Now))
        Log("OFF")
    Else
        Log(DateTime.Time(DateTime.Now))
        Log("ON")
    End If
 
Upvote 0
Top