How to round milliseconds of datetime Format?
Show older comments
Here's an example:
>> dt = datetime("098:17:17:34.475", 'InputFormat', 'DDD:HH:mm:ss.SSS', 'Format', 'DDD:HH:mm:ss.SSS')
dt =
datetime
098:17:17:34.475
>> SecsSinceNewYear = seconds(dt - datetime(dt.Year, 1, 0, 0, 0, 0, 0))
SecsSinceNewYear =
8529454.475
>> dt = datetime(0, 1, 0, 0, 0, SecsSinceNewYear, 'Format', 'DDD:HH:mm:ss.SSS')
dt =
datetime
098:17:17:34.474
>> string(dt)
ans =
"098:17:17:34.474"
>> dt.Format = 'DDD:HH:mm:ss.SSSSSSSSS'
dt =
datetime
098:17:17:34.474999999
Notice that the input ends in 475 milliseconds, but when displayed there seems to be a precision error that ends up not rounding and displays 474 milliseconds. So when I attempt to convert the datetime back into it's original string form the millisecnds have changed from 475 to 474.
Is there anyway to avoid this error introduced by converting these formats?
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!