rounded time value with intervals
Show older comments
Im trying to convey the seconds since midnight into the format HH:MM PM (or AM). So far I have this:
secondsSinceMidnight = 26850.431
minuteInterval = 15
datestr(seconds(timestamp),'HH:MM PM')
The problem is that when it's an time like '7:27 AM', it needs to say '07:30 AM' ( i.e. have the zero in front and round up to closest 15 min interval).
How would I go about those two fixes?
Accepted Answer
More Answers (1)
Cris LaPierre
on 20 Feb 2022
Edited: Cris LaPierre
on 21 Feb 2022
I think in this case, it's easiest to round the seconds to the closest 15-minute increment. This uses 24-hr time, so no AM/PM is necessary.
minuteInterval = 15;
secondsSinceMidnight = 26850.431;
% calculate the closest 15-minute increment
seconds15min = round(secondsSinceMidnight/minuteInterval/60)*minuteInterval*60;
% Convert to a duratoin
t = seconds(seconds15min)
% format the time
t.Format = 'hh:mm'
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!