How do I round a 'datetime' to the nearest second?

I have the following date and time:
2023-11-07 23:47:43.605
I can construct a 'datetime' for this as such:
>> d = '2023-11-07 23:47:43.605'; >> t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS') t =    datetime    07-Nov-2023 23:47:43
I would like to round this to the nearest second to receive the following output after displaying:
2023-11-07 23:47:44
How do I do this?

 采纳的回答

You can use the function “round” to change the actual value of the 'datetime' to round to the nearest second. An example of how to implement this is shown below:
>> d = '2023-11-07 23:47:43.605'; >> t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS'); >> t.Second = round(t.Second) t =    datetime    07-Nov-2023 23:47:44
For more information about the MATLAB function “round” please see the following link:

更多回答(0 个)

类别

在 帮助中心 和 File 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