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?

 Accepted Answer

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:

More Answers (0)

Categories

Products

Release

No release entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!