Find time between 2 given time
Afficher commentaires plus anciens
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 commentaires
Ankit
le 27 Avr 2022
dt = datetime;
datesonly = [dt.Day dt.Month dt.Year];
timesOnly = [dt.Hour dt.Minute dt.Second];
dpb
le 27 Avr 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.
Réponse acceptée
Plus de réponses (2)
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
le 28 Avr 2022
0 votes
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 commentaire
ahmad
le 28 Avr 2022
https://de.mathworks.com/matlabcentral/answers/300711-how-to-convert-data-in-utc-to-a-specific-local-time
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!