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
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
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
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 !!
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
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
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
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