how to floor time for each hour?

조회 수: 6 (최근 30일)
Lilya
Lilya 2022년 3월 15일
댓글: Lilya 2022년 3월 17일
Hi Experts,
I have this time format, and i want to have the time only for each hour. Could anyone please help with this?
'12-Dec-2021 09:09:04'
'12-Dec-2021 09:24:04'
'12-Dec-2021 09:39:04'
'12-Dec-2021 09:54:04'
Much appreciated
  댓글 수: 2
KSSV
KSSV 2022년 3월 15일
What's your desired output?
Lilya
Lilya 2022년 3월 16일
to have the data at each hour instead of having different time steps for each hour

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

채택된 답변

Star Strider
Star Strider 2022년 3월 15일
Try this —
tv = {'12-Dec-2021 09:09:04'
'12-Dec-2021 09:24:04'
'12-Dec-2021 09:39:04'
'12-Dec-2021 09:54:04'};
dtv = datetime(tv, 'Format','dd-MMM-yyyy HH') % Display Only The Hour
dtv = 4×1 datetime array
12-Dec-2021 09 12-Dec-2021 09 12-Dec-2021 09 12-Dec-2021 09
dtv = datetime(tv, 'Format','dd-MMM-yyyy HH:00:00') % Display Hour, Set Rest To Zeros
dtv = 4×1 datetime array
12-Dec-2021 09:00:00 12-Dec-2021 09:00:00 12-Dec-2021 09:00:00 12-Dec-2021 09:00:00
The 'Format' name-value pair controls the display, so set it however you want it.
.
  댓글 수: 3
Star Strider
Star Strider 2022년 3월 16일
편집: Star Strider 2022년 3월 16일
As always, my pleasure!
EDIT — (16 Mar 2022 at 12:40)
To summarise the data for each hour, use the table2timetable function and then the retime function.
.
Lilya
Lilya 2022년 3월 16일
Noted

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

추가 답변 (2개)

Steven Lord
Steven Lord 2022년 3월 15일
If you want to actually modify the data rather than just changing how the data is displayed you can use dateshift.
tv = {'12-Dec-2021 09:00:00',
'12-Dec-2021 09:09:04'
'12-Dec-2021 09:24:04'
'12-Dec-2021 09:39:04'
'12-Dec-2021 09:54:04'};
dtv = datetime(tv)
dtv = 5×1 datetime array
12-Dec-2021 09:00:00 12-Dec-2021 09:09:04 12-Dec-2021 09:24:04 12-Dec-2021 09:39:04 12-Dec-2021 09:54:04
dtv2 = dateshift(dtv, 'start', 'hour')
dtv2 = 5×1 datetime array
12-Dec-2021 09:00:00 12-Dec-2021 09:00:00 12-Dec-2021 09:00:00 12-Dec-2021 09:00:00 12-Dec-2021 09:00:00
To check that the data has actually been changed:
dtv2 == dtv(1)
ans = 5×1 logical array
1 1 1 1 1
  댓글 수: 3
Steven Lord
Steven Lord 2022년 3월 16일
Based on your answer to KSSV's comment "to have the data at each hour instead of having different time steps for each hour" I suspect that storing your data in a timetable array and using retime to retime it to an hourly basis might be a solution to your larger problem.
Lilya
Lilya 2022년 3월 17일
yes.. I did that and it works perfectly!
thank you very very much.

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


Arif Hoq
Arif Hoq 2022년 3월 15일
A={'12-Dec-2021 09:09:04'
'12-Dec-2021 09:24:04'
'12-Dec-2021 09:39:04'
'12-Dec-2021 09:54:04'};
B=datetime(A,'Format','hh') % if only hour
B1=datetime(A,'Format','hh:mm:ss') % if hour,min,sec
  댓글 수: 1
Lilya
Lilya 2022년 3월 16일
Thanks Arif.. appreciated

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

카테고리

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