How to round datetime array to nearest minute?
52 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ziye
el 14 de Sept. de 2017
Comentada: Peter Perkins
el 14 de Sept. de 2017
Sounds like quite a easy question but i've spent a lot of time and cant find the answer. Basically I have a datetime array, i want to reduce the precision to minute so that second and beyond(millisecond etc.) are all changed to zero. is there any fast way to do so? Many Thanks
0 comentarios
Respuesta aceptada
Steven Lord
el 14 de Sept. de 2017
You can use the dateshift function to do this. If you always want the result to be no later than the input, use 'start' on its own.
% Create some sample data
sampleData = datetime('now')
% Modify the display format of the sample data to show fractional seconds
sampleData.Format = 'dd-MMM-uuuu HH:mm:ss.SSS'
% Shift it to the start of the minute, throwing away seconds and fractional seconds
startOfMinute = dateshift(sampleData, 'start', 'minute')
If you want the result to be rounded to the start of the nearest minute, which could be later than the input datetime, use 'start' with 'nearest'.
% Create a sample datetime in the second half of the minute and set its format
sampleData = datetime(2017, 9, 14, 09, 52, 47.234, 'Format', ...
'dd-MMM-uuuu HH:mm:ss.SSS')
% Rounding should give 9:53 on September 14, 2017 since
% that's closer to sampleData than 9:52 on September 14, 2017
result = dateshift(sampleData, 'start', 'minute', 'nearest')
0 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!