Rounding Time to next half hour
Show older comments
I have time stamps at one second increments (9/8/2015 14:31:01, 9/8/2015 14:31:02, 9/8/2015 14:31:03...) that I would like to round up to the next half hour. So 9/8/2015 14:31:01 would be 9/8/2015 15:00:00 9/8/2015 14:29:01 would be 9/8/2015 14:30:00 and so on. Is there something similar to Excel's ceiling function where I can specify to what fraction I want to round up to?
Accepted Answer
More Answers (1)
Peter Perkins
on 10 Sep 2015
In MATLAB R2014b or later:
>> d1 = datetime({'9/8/2015 14:31:01', '9/8/2015 14:31:02', '9/8/2015 14:31:03'})
d1 =
08-Sep-2015 14:31:01 08-Sep-2015 14:31:02 08-Sep-2015 14:31:03
>> d2 = dateshift(d1,'start','hour') + minutes(30)
d2 =
08-Sep-2015 14:30:00 08-Sep-2015 14:30:00 08-Sep-2015 14:30:00
1 Comment
Trung Hieu Le
on 17 Jun 2016
Hi Peter,
Could you help me revise the code to receive the rounding time to next half hour as my required output?
>> d1 = datetime({'9/8/2015 14:46:01'})
My required output:
08-Sep-2015 15:00:00
If I use your code:
>> d2 = dateshift(d1,'start','hour') + minutes(30)
The result will be 14:30:00.
If I revise the code:
d2 = dateshift(d1,'start','hour') + minutes(60)
I will receive the right answer, however, do you know which code will be automatically for this?
Thanks!
Categories
Find more on Time Series Objects 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!