Compact way to extract day and hour from a datetime / datestrings format ?
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 27 Jun 2022
Edited: Voss
on 27 Jun 2022
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 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!