Compact way to extract day and hour from a datetime / datestrings format ?

4 次查看(过去 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 个评论
Sim
Sim 2022-6-28
编辑:Sim 2022-6-28
Thanks a lot @KSSV, very kind!
I have realised that my question was not clear and I am really sorry!
Your solution answers my question in the title, and
t.Day
gives me the day that I select from
t
For example:
>> t.Day(200)
ans =
25
gives me "25" because "t(200)" corresponds to '25-Jun-2017 07:00:00',
>> t(200)
ans =
datetime
25-Jun-2017 07:00:00
However, in my question description, I asked something slightly different.. Probably, I should rephrase the question...?
What I wanted to know was the day's number corresponding to "t(200)", starting from the first day in my dataset "t(1)"
>> t(1)
ans =
datetime
17-Jun-2017
In this example, the 25th of June would be the 9th day, if we start counting from the first day of my dataset, i.e. the 17th of June (the 17th of June would be the day number 1, the 18th of June would be the day number 2, and so on...).
In this regard, @Voss provided one possible answer / solution, i.e.
>> floor(days(t(200)-t(1))) + 1
ans =
9

请先登录,再进行评论。

采纳的回答

Voss
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)
t = 7×1 datetime array
17-Jun-2017 00:00:00 17-Jun-2017 01:00:00 17-Jun-2017 02:00:00 17-Jun-2017 23:00:00 18-Jun-2017 00:00:00 25-Jun-2017 07:00:00 31-Jul-2017 13:00:00
disp(t(end-1));
25-Jun-2017 07:00:00
floor(days(t(end-1)-t(1))) + 1
ans = 9
t(end-1).Hour
ans = 7
disp(t(end));
31-Jul-2017 13:00:00
floor(days(t(end)-t(1))) + 1
ans = 45
t(end).Hour
ans = 13

更多回答(1 个)

Sim
Sim 2022-6-28
编辑:Sim 2022-6-28
Thanks a lot @Voss, your code does what I need... In my case, I did in this way and it works:
t = datetime(time_array); % I found out that "time_array" was not in a "datetime" format
index_dataset = 200; % just an example
number_of_days_from_the_first_one = floor(days(t(index_dataset)-t(1))) + 1;
hour_corresponding_to_index_dataset = t(index_dataset).Hour;

类别

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