필터 지우기
필터 지우기

search interval in datetime

조회 수: 4 (최근 30일)
Luca Re
Luca Re 2024년 5월 20일
댓글: Luca Re 2024년 5월 21일
hi, I have a datetime array of several many days. I need to serch interval
example:
TimeA=22:00
TimeB=03:00
search datetime>=timeA and datetime<=timeB (22:00 ..22:01..22:02. etc......23:59 ..00:00...00:01.etc... 2:58..2:59 3:00)
So I want without checking the day to have the indices of that range in the whole array
(the times in datetime are already ordered in a correct chronological way so the next time is always after the previous one)

채택된 답변

Steven Lord
Steven Lord 2024년 5월 21일
Use the timeofday function to return the time since midnight of each element of the datetime array.
T = datetime('today') + hours(24*rand(10, 1))
T = 10x1 datetime array
21-May-2024 00:01:16 21-May-2024 10:22:46 21-May-2024 07:40:50 21-May-2024 23:22:42 21-May-2024 10:42:10 21-May-2024 20:30:07 21-May-2024 22:42:34 21-May-2024 13:15:17 21-May-2024 06:45:24 21-May-2024 06:13:04
TOD = timeofday(T)
TOD = 10x1 duration array
00:01:16 10:22:46 07:40:50 23:22:42 10:42:10 20:30:07 22:42:34 13:15:17 06:45:24 06:13:04
Then compare to duration objects representing the ends of the interval of interest. In this case because the interval includes midnight we need to ask if the timeofday is greater than the first end or less than the second end.
past2200 = TOD > duration(22, 0, 0)
past2200 = 10x1 logical array
0 0 0 1 0 0 1 0 0 0
before0300 = TOD < duration(3, 0, 0)
before0300 = 10x1 logical array
1 0 0 0 0 0 0 0 0 0
T(past2200 | before0300)
ans = 3x1 datetime array
21-May-2024 00:01:16 21-May-2024 23:22:42 21-May-2024 22:42:34
results = table(T, past2200, before0300, past2200 | before0300, ...
'VariableNames', ["times", "> 2200", "< 0300", "overnight"])
results = 10x4 table
times > 2200 < 0300 overnight ____________________ ______ ______ _________ 21-May-2024 00:01:16 false true true 21-May-2024 10:22:46 false false false 21-May-2024 07:40:50 false false false 21-May-2024 23:22:42 true false true 21-May-2024 10:42:10 false false false 21-May-2024 20:30:07 false false false 21-May-2024 22:42:34 true false true 21-May-2024 13:15:17 false false false 21-May-2024 06:45:24 false false false 21-May-2024 06:13:04 false false false
  댓글 수: 3
Steven Lord
Steven Lord 2024년 5월 21일
Use the ymd function to obtain the year, month, and day components of the datetime.
dt = datetime('now')
dt = datetime
21-May-2024 15:02:57
[y, m, d] = ymd(dt)
y = 2024
m = 5
d = 21
There's a similar hms function for hour, minute, and second.
[h, m2, s] = hms(dt)
h = 15
m2 = 2
s = 57.0178
Luca Re
Luca Re 2024년 5월 21일
oky thank

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by