Find time between 2 given time
4 views (last 30 days)
I wanted to get date and time separately, from the system time t
t = datetime('now')
Then, I wanted to find if the system time falls between 2 given time, as in the variable hrs.mat
I wanted to get that particular column number
if the current system time is 9:00:00 i wanted to get the ans = 1
if the current system time is 14:40:23 i wanted to get the ans = 5
else if my current system time is 16:55:44 then i need to display a message something like "Time Over"
Or if it is not possible to get with the answer with the format in the attached variable hrs.mat
Please suggest o which time format i should change the hrs.mat so as to get the answer i want
Edited: dpb on 28 Apr 2022
OK, the above is one simple-minded approach that might work depending on the needs...another that works with the given data and discrete bounds is
binData=datetime(split(hrs.','-'),'InputFormat','HH:mm'); % create discrete bins as datetime
h=(datetime('now')); % current time
idx=find(isbetween(h,bindata(:,1),bindata(:,2))) % lookup current bin location
>> h=h+hours(9) % test outside range
>> idx=find(isbetween(h,ans(:,1),ans(:,2))) % is empty result if not in defined bin
0×1 empty double column vector
The above requires to create the bins for the same day as testing against current time.
It also will show the above Q? re: the non-contiguous boundaries as failing so the 10:05 example given will also return the empty indicator. To resolve this, one would need to make the lower bin edges all begin on the end time of the preceding bin. Of course, if the short 10 minute time interval is intended to be out, then this will catch that as well, but one presumes that is probably not the intent but just a result of the rounding to 10-minute intervals.
More Answers (2)
Edited: dpb on 28 Apr 2022
You're overthinking the problem methinks... :)
hrs=str2double(extractBefore(extractBefore(hrs,'-'),':')); % convert the interval start to numeric hour
h=hour(datetime('now')); % get the present hour for comparison
idx=interp1(hrs,1:numel(hrs),h,'previous'); % lookup the index
if isnan(idx), 'Out of Range', end % outside table range will be NaN
This could be encapsulated in a little function to handle error condition, etc., but the general outline is as shown -- there really isn't any need for the hours as time data at all in this problem other than to identify the bins if they are not always consistent values. That is, you could simply write tBins=[9:
The above does assume there are not any gaps in the time range of interest; if that were to be possible there would have to be logic added to identify those gaps.
This also does assume that the local time and the time of the data are in same time zone; an adjustment for the TZ correction would have to be made on either the input time or the table to account for that if were to be possible or the function needed to be able to cope with such.
I overlooked the hour gap of 12:00 hour -- the above will return the 11:00 index for such -- is this a problem? If so, then will need the interval width as well. That also introduces the issue that the time bins given are not inclusive so that there's a gap between 10:00 the upper limit of bin 1 and 10:10, the lower limit of bin 2. What's to happen there when the current time is 10:05, say?
ahmad on 28 Apr 2022
In datetime function you have to use TimeZone option to now the time in your hrs.mat file. So you have to add some more information in your hrs.mat file which specified the TimeZone of your data.