Find time between 2 given time

19 views (last 30 days)
Elysi Cochin
Elysi Cochin on 27 Apr 2022
Commented: Elysi Cochin on 28 Apr 2022
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
For eg:
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
  2 Comments
Ankit
Ankit on 27 Apr 2022
dt = datetime;
datesonly = [dt.Day dt.Month dt.Year];
timesOnly = [dt.Hour dt.Minute dt.Second];
dpb
dpb on 27 Apr 2022
[y,m,d]=ymd(t);
[h,mn,s]=hms(t);
...
fname = sprintf('%d-%d-%d.xlsx', d,m,y);
if exist(fname,'file')
....
end
NB:
Good practice would be to use %02d in the pattern creating and reading file names of this type -- so that ordinal sorting is consistent with textual -- and, then to aid in that going by year,mont,date instead of by day so will also be in calendar order in directory listings.
NB Second:
Good practice would also consist of using fullfile and fully-qualified file names.
NB Third:
Can also pass above file name variable to dir() and check for empty returned stuct array besides exist shown.

Sign in to comment.

Accepted Answer

dpb
dpb on 28 Apr 2022
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
load hrs
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
idx =
2
>> h=h+hours(9) % test outside range
h =
datetime
28-Apr-2022 19:49:41
>> idx=find(isbetween(h,ans(:,1),ans(:,2))) % is empty result if not in defined bin
idx =
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)

dpb
dpb on 28 Apr 2022
Edited: dpb on 28 Apr 2022
You're overthinking the problem methinks... :)
load hrs
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.
ADDENDUM:
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
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.
  1 Comment
ahmad
ahmad on 28 Apr 2022
https://de.mathworks.com/matlabcentral/answers/300711-how-to-convert-data-in-utc-to-a-specific-local-time

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!