How to round datetime array to nearest minute?
50 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Steven Lord
2017 年 9 月 14 日
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 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!