no need to parse date because I allready have the ticks and i can easy get hour with datetme.gethour
I will try to reformulate :
I press button and records the time ticks . Ex : I press button at 9 o clock ...date not mather
Then I will press button again after some days ...I what to see if this time it was in same time span with the earlier record .
Translating the sentence above:
I press button after some days after the first event ...if this time is in hours between 8 and 10 ..then Bang!!!!
First day, you press the button:
FirstPressTimeTicks = DateTime.Now - NowDayTicks
save this somewhere
thrid day you press the button, get new ticks
SecondPressTimeTicks = DateTime.Now - NowDayTicks
Now compare with earlier record:
if SecondPressTimeTicks > (FirstPressTimeTicks - (1 x DateTime.TicksPerHour) AND SecondPressTimeTicks < (FirstPressTimeTicks + (1 x DateTime.TicksPerHour)
so if FirstPressTimeTicks = 9
then it checks if Second > (9 - 1) and Second < (9 + 1)
Use DateUtils.PeriodBetween it will return a Period object with the number of days, hours and minutes between the two time instances. You should ignore the days and compare the other fields.
How can I use hours,minutes and seconds field to see if the second time 2 o'clock is a timespan of 10 minutes from 23 a 'clock
23 o'clock-10 minutes < 2 o'clock < 23 o'clock +10 minute ??
first time(hour) -10minutes < second time(hour) < first time(hour)+10 minutes
I wrote the code below , now i have to test it ...does it look ok ?
B4X:
Sub intime (lasttime As Long,span_minutes As Int) As Boolean
Dim tfx As Long
tfx=lasttime-DateTime.GetHour(lasttime)*DateTime.TicksPerHour ' calculates ticks elapsed from 0 o'clock lasttime date
Dim tfy As Long
tfy=DateTime.Now-DateTime.GetHour(DateTime.Now)*DateTime.TicksPerHour ' calculates ticks elapsed from 0 o'clock now date
' now brings last time to present minus hours diference
Dim lasttime_present As Long
lasttime_present= lasttime+(tfy-tfx) ' lasttime ticks + date diference
'checks the normal expire time routine as lastime is happens now but at diferent hour clock
If DateTime.Now < lasttime_present + span_minutes*DateTime.TicksPerMinute AND DateTime.Now> lasttime_present - span_minutes*DateTime.TicksPerMinute Then
Return True
end sub
End If
Sub intime (lasttimex As Long,span_minute As Int) As Boolean
Dim tfx As Long
tfx=lasttimex-DateTime.GetHour(lasttimex)*DateTime.TicksPerHour - DateTime.GetMinute(lasttimex)*DateTime.TicksPerMinute-DateTime.GetSecond(lasttimex)*DateTime.TicksPerSecond ' calculates ticks elapsed from 0 o'clock lasttime date
Log("tfx:"&tfx)
Log("orax"&DateTime.GetHour(lasttimex))
Log("minx"&DateTime.GetMinute(lasttimex))
Log("secx"&DateTime.GetSecond(lasttimex))
Dim tfy As Long
tfy=DateTime.Now-DateTime.GetHour(DateTime.Now)*DateTime.TicksPerHour-DateTime.GetMinute(DateTime.Now)*DateTime.TicksPerMinute-DateTime.GetSecond(DateTime.Now)*DateTime.TicksPerSecond ' calculates ticks elapsed from 0 o'clock now date
Log("tfy:"&tfy)
Log("ora_now"&DateTime.GetHour(DateTime.Now))
Log("min_now"&DateTime.GetMinute(DateTime.Now))
Log("sec_now"&DateTime.GetSecond(DateTime.Now))
' now brings last time to present minus hours diference
Dim lasttime_present As Long
lasttime_present= lasttimex+(tfy-tfx) ' lasttime ticks + date diference
Log("lasttime_present:" &lasttime_present)
Log(DateTime.Time(lasttime_present))
Log(DateTime.Time(DateTime.Now))
'checks the normal expire time routine as lastime is happens now but at diferent hour clock
If DateTime.Now < lasttime_present + span_minute*DateTime.TicksPerMinute AND DateTime.Now> lasttime_present - span_minute*DateTime.TicksPerMinute Then
Return True
End If
End Sub
The purpose of my question !
User set proximity alert city x from 6 to 9 o'clock. year 2012 was the time of first location and time based alarm.
Each time user passed city x and the time is between 6 and 9 o'clock an alarm is triggered.
This code snippet checks the difference in minutes between two times ignoring days (seconds will be ignored at the moment)
dt1 is first DateTime, dt2 is second DateTime, maxRange ist the max. difference in minutes to check.
B4X:
Sub Time_In_Range(dt1 As Long, dt2 As Long, maxRange As Int) As Boolean
Dim p As Period = DateUtils.PeriodBetween(dt1, dt2)
Dim diff As Long = Abs(p.Hours) * 60 + Abs(p.Minutes)
If Min(diff, 1440 - diff) <= maxRange Then
Return True
Else
Return False
End If
End Sub