Find time between 2 given time
조회 수: 16 (최근 30일)
이전 댓글 표시
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
Ankit
2022년 4월 27일
dt = datetime;
datesonly = [dt.Day dt.Month dt.Year];
timesOnly = [dt.Hour dt.Minute dt.Second];
dpb
2022년 4월 27일
[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.
채택된 답변
dpb
2022년 4월 28일
편집: dpb
2022년 4월 28일
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.
추가 답변 (2개)
dpb
2022년 4월 28일
편집: dpb
2022년 4월 28일
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?
댓글 수: 0
ahmad
2022년 4월 28일
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
ahmad
2022년 4월 28일
https://de.mathworks.com/matlabcentral/answers/300711-how-to-convert-data-in-utc-to-a-specific-local-time
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!