How to extract hour+minute from DateTime vector ?
12 ビュー (過去 30 日間)
古いコメントを表示
Q: how do I best extract BOTH the hour + minute from a DateTime vector in MATLAB ? My DateTime format is: 15-Apr-2016 11:43:11
I need to fine-tune the rush hours to the exact time ranges below. My current code, below, works but does not include the 'minutes'. Please help.
t = datetime(data.DateTime);d = day(t,'dayofyear'); DayofYr = d;
tf = isweekend(t); % returns logical 1 = true = weekend, else 0
Wkend = tf;
h = hour(data.DateTime); % extract hour from Datetime vector
isAMRush = h>=7 & h<9 ==1; % needs to be revised to: 745a -845a morning rush
isPMRush = h>=15 & h<17 ==1; % needs to be revised to: 330p -430p afternoon rush
%
2 件のコメント
Stephen23
2022 年 6 月 8 日
D = datetime(2016,4,5,[8;11],43,11)
isAMRush = isbetween(timeofday(D),duration(7,45,0),duration(8,45,0))
採用された回答
Stephen23
2022 年 6 月 8 日
D = datetime(2016,4,5,[8;11],43,11)
isAMRush = isbetween(timeofday(D),duration(7,45,0),duration(8,45,0))
1 件のコメント
Peter Perkins
2022 年 6 月 13 日
Right.
I mean the real answer is, "you hardly ever need to actually explicitly extract individual time components."
その他の回答 (2 件)
dpb
2022 年 6 月 8 日
Convert to durations and use
isAMRush=iswithin(duration(hour(t),minute(t),0),duration(7,45,0),duration(8,45,0));
Steven Lord
2022 年 6 月 8 日
d = datetime('15-Apr-2016 11:43:11')
[h, m, s] = hms(d)
or
tod = timeofday(d)
0 件のコメント
参考
カテゴリ
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!