Compact way to extract day and hour from a datetime / datestrings format ?
2 ビュー (過去 30 日間)
古いコメントを表示
I have a cell array including datetime / datestrings data, i.e. days and hours.
Here following, I show two examples related to the same day, and 24 hours far from each other:
>> time_array(1)
ans =
1×1 cell array
{'17-Jun-2017 0:00:00'}
>> time_array(24)
ans =
1×1 cell array
{'17-Jun-2017 23:00:00'}
Is there any compact way to extract day number (starting from the first day in my dataset) and hour just given the row / index of my dataset, called time_array?
For example, if I give the row / index of my datset, let's say 200:
>> time_array(200)
ans =
1×1 cell array
{'25-Jun-2017 7:00:00'}
is there any compact way to get this output ?
day = 9
hour = 7
Indeed, the 25th June is the day number 9 if we start to count from the day number 1, which is the 17th June.
2 件のコメント
採用された回答
Voss
2022 年 6 月 27 日
編集済み: Voss
2022 年 6 月 27 日
time_array = { ...
'17-Jun-2017 0:00:00'; ...
'17-Jun-2017 1:00:00'; ...
'17-Jun-2017 2:00:00'; ...
...
'17-Jun-2017 23:00:00'; ...
'18-Jun-2017 0:00:00'; ...
...
'25-Jun-2017 7:00:00'; ...
...
'31-Jul-2017 13:00:00'; ...
};
t = datetime(time_array)
disp(t(end-1));
floor(days(t(end-1)-t(1))) + 1
t(end-1).Hour
disp(t(end));
floor(days(t(end)-t(1))) + 1
t(end).Hour
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!