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
 

ilan

Expert
Licensed User
Longtime User
doesn't work for me that option

it does not make any sense to me. can you explain exactly what you want to archive like when should the OFF be shown.
between what time? between 16:18 to 7:15 ?
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
ok, What I need is: the restaurant will be closed between 21:30 until 10:00
then people can not make orders using our mobile-App, when the restaurant is closed.

Actually the manager have to click a button in our MasterApp in the cafe,
but sometimes they forgot to click the button to close the option to receive orders,
This is why I want an automatic option.

I tested your code just between minutes, example 19:58 and 20:02 and it does work using:
(work means: I see the log OFF or ON when the condition is true)
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


but does not work using OR or AND in this code:
B4X:
    Dim OffHour1 = 18, OffMinute1 = 13, OffHour2 = 19, OffMinute2 = 16  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 And nowTime < offTime2 Then
        Log(DateTime.Time(DateTime.Now))
        Log("OFF")
    Else
        Log(DateTime.Time(DateTime.Now))
        Log("ON")
    End If

of course, maybe I'm testing wrong... will do more tests soon..
thanks @ilan
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You can check whether the time is fall between StartTime and EndTime.
B4X:
Sub InBetween(Start As Long, EndTime As Long) As Boolean
  Return DateTime.Now >= Start AND DateTime.Now <= EndTime
End Sub
Taken from Erel answer: https://www.b4x.com/android/forum/t...lues-with-a-between-keyword.31152/post-181333

Then apply in your code like this:
B4X:
    Dim yy As Int = DateTime.GetYear(DateTime.Now)
    Dim mm As Int = DateTime.GetMonth(DateTime.Now)
    Dim dd As Int = DateTime.GetDayOfMonth(DateTime.Now)
    Dim StartTime, EndTime As Long
    StartTime = DateUtils.SetDateAndTime(yy, mm, dd, 10, 00, 0)
    EndTime = DateUtils.SetDateAndTime(yy, mm, dd, 21, 30, 0)
    
    If InBetween(StartTime, EndTime) Then
        Log("We can take order now")
    End If
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i have just tested the code and it works fine

B4X:
    Dim OffHour1 = 18, OffMinute1 = 50, OffHour2 = 13, OffMinute2 = 16  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(DateTime.Time(DateTime.Now))
    Log("OFF")
        Else
    Log(DateTime.Time(DateTime.Now))
        Log("ON")
    End If

how are you checking the code?
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
You can check whether the time is fall between StartTime and EndTime.
B4X:
Sub InBetween(Start As Long, EndTime As Long) As Boolean
  Return DateTime.Now >= Start AND DateTime.Now <= EndTime
End Sub
Taken from Erel answer: https://www.b4x.com/android/forum/t...lues-with-a-between-keyword.31152/post-181333

Then apply in your code like this:
B4X:
    Dim yy As Int = DateTime.GetYear(DateTime.Now)
    Dim mm As Int = DateTime.GetMonth(DateTime.Now)
    Dim dd As Int = DateTime.GetDayOfMonth(DateTime.Now)
    Dim StartTime, EndTime As Long
    StartTime = DateUtils.SetDateAndTime(yy, mm, dd, 10, 00, 0)
    EndTime = DateUtils.SetDateAndTime(yy, mm, dd, 21, 30, 0)
   
    If InBetween(StartTime, EndTime) Then
        Log("We can take order now")
    End If
ok thanks.. will test tomorrow..sleep time here.
once again thanks @aeric
 
Upvote 0

udg

Expert
Licensed User
Longtime User
From your description I got that you don't need to consider time zones and similar time related problems.
But, since you necessarily have a server where orders are sent, why not letting the server itself open/close the virtual shop (i.e. accept orders)?
This way you don't risk that a user could manually alter its local time and send an order when the shop is closed.

Just my 2c
 
Upvote 0
Top