How do I round this datetime to the nearest 0.1 second?
91 次查看(过去 30 天)
显示 更早的评论
I have the following datetime: 01-Oct-2020 04:49:10.350
How would I use MATLAB to round the millisecond value to the nearest tenth? i.e. I want the datetime to be rounded to: 01-Oct-2020 04:49:10.4
I know there is the dateshift function in MATLAB but the lowest you can go is rounding to seconds. Is there a way to go even further?
0 个评论
采纳的回答
Adam Danz
2021-11-18
dt = datetime(' 01-Oct-2020 04:49:10.350','Format', 'dd-MMM-uuuu HH:mm:ss.SSS')
dtRound = dateshift(dt,'start','minute') + seconds(round(second(dt),1))
更多回答(1 个)
the cyclist
2021-11-18
编辑:the cyclist
2021-11-18
This is awkward, but it works. Guessing there is a better way.
% Define original datetime input
dt = datetime('01-Oct-2020 04:49:10.350');
% Define new datetime, by calculating difference from rounded seconds, and
% incrementing
new_dt = dt + duration([0 0 round(second(dt),1) - second(dt)]);
% Show that the new datetime is incremented
seconds(new_dt - dt)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!