how to floor time for each hour?
8 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
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 = datetime(tv, 'Format','dd-MMM-yyyy HH:00:00') % Display Hour, Set Rest To Zeros
.
3 件のコメント
Star Strider
2022 年 3 月 16 日
編集済み: Star Strider
2022 年 3 月 16 日
As always, my pleasure!
EDIT — (16 Mar 2022 at 12:40)
.
その他の回答 (2 件)
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)
dtv2 = dateshift(dtv, 'start', 'hour')
To check that the data has actually been changed:
dtv2 == dtv(1)
3 件のコメント
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.
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
参考
カテゴリ
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!