# [ACCEPTED]-Round NSDate to the nearest 5 minutes-cocoa

Score: 62

Here's my solution:

``````NSTimeInterval seconds = round([date timeIntervalSinceReferenceDate]/300.0)*300.0;
NSDate *rounded = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
``````

I did some testing and 10 it is about ten times as fast as Voss's 9 solution. With 1M iterations it took about 8 3.39 seconds. This one performed in 0.38 7 seconds. J3RM's solution took 0.50 seconds. Memory 6 usage should be the lowest also.

Not that 5 the performance is everything but it's a 4 one-liner. Also you can easily control the 3 rounding with division and multiplication.

EDIT: To 2 answer the question, you can use `ceil` to round 1 up properly:

``````NSTimeInterval seconds = ceil([date timeIntervalSinceReferenceDate]/300.0)*300.0;
NSDate *rounded = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
``````

EDIT: An extension in Swift:

``````public extension Date {

public func round(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .toNearestOrAwayFromZero)
}

public func ceil(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .up)
}

public func floor(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .down)
}

private func round(precision: TimeInterval, rule: FloatingPointRoundingRule) -> Date {
let seconds = (self.timeIntervalSinceReferenceDate / precision).rounded(rule) *  precision;
return Date(timeIntervalSinceReferenceDate: seconds)
}
}
``````
Score: 55

Take the minute value, divide by 5 rounding 3 up to get the next highest 5 minute unit, multiply 2 to 5 to get that back into in minutes, and 1 construct a new NSDate.

``````NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:curDate];
NSInteger minutes = [time minute];
float minuteUnit = ceil((float) minutes / 5.0);
minutes = minuteUnit * 5.0;
[time setMinute: minutes];
curDate = [[NSCalendar currentCalendar] dateFromComponents:time];
``````
Score: 40

``````import UIKit

enum DateRoundingType {
case round
case ceil
case floor
}

extension Date {
func rounded(minutes: TimeInterval, rounding: DateRoundingType = .round) -> Date {
return rounded(seconds: minutes * 60, rounding: rounding)
}
func rounded(seconds: TimeInterval, rounding: DateRoundingType = .round) -> Date {
var roundedInterval: TimeInterval = 0
switch rounding  {
case .round:
roundedInterval = (timeIntervalSinceReferenceDate / seconds).rounded() * seconds
case .ceil:
roundedInterval = ceil(timeIntervalSinceReferenceDate / seconds) * seconds
case .floor:
roundedInterval = floor(timeIntervalSinceReferenceDate / seconds) * seconds
}
return Date(timeIntervalSinceReferenceDate: roundedInterval)
}
}

// Example

let nextFiveMinuteIntervalDate = Date().rounded(minutes: 5, rounding: .ceil)
print(nextFiveMinuteIntervalDate)
``````

0

Score: 33

Wowsers, I see a lot of answers here, but 12 many are long or difficult to understand, so 11 I'll try to throw in my 2 cents in case 10 it helps. The `NSCalendar` class provides the functionality 9 needed, in a safe and concise manner. Here 8 is a solution that works for me, without 7 multiplying time interval seconds, rounding, or 6 anything. `NSCalendar` takes into account leap days/years, and 5 other time and date oddities. (Swift 2.2)

``````let calendar = NSCalendar.currentCalendar()
let rightNow = NSDate()
let interval = 15
let nextDiff = interval - calendar.component(.Minute, fromDate: rightNow) % interval
let nextDate = calendar.dateByAddingUnit(.Minute, value: nextDiff, toDate: rightNow, options: []) ?? NSDate()
``````

It 4 can be added to an extension on `NSDate` if needed, or 3 as a free-form function returning a new 2 `NSDate` instance, whatever you need. Hope this 1 helps anyone who needs it.

Swift 3 Update

``````let calendar = Calendar.current
let rightNow = Date()
let interval = 15
let nextDiff = interval - calendar.component(.minute, from: rightNow) % interval
let nextDate = calendar.date(byAdding: .minute, value: nextDiff, to: rightNow) ?? Date()
``````
Score: 14

see link for full and detailed answer from 2 an Apple staff member. To save you a click, the 1 solution:

``````let original = Date()

let rounded = Date(timeIntervalSinceReferenceDate:
(original.timeIntervalSinceReferenceDate / 300.0).rounded(.toNearestOrEven) * 300.0)
``````
Score: 13

I think this is the best solution, but just 4 my opinion, based on previous poster code. rounds 3 to nearest 5 min mark. This code should 2 use a lot less memory than the date components 1 solutions. Brilliant, Thanks for the direction.

``````+(NSDate *) dateRoundedDownTo5Minutes:(NSDate *)dt{
int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval % 300;
int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds;
if(remainingSeconds>150)
{/// round up
timeRoundedTo5Minutes = referenceTimeInterval +(300-remainingSeconds);
}
NSDate *roundedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
return roundedDate;
}
``````
Score: 6

Thanks for the sample. Below I have added 1 some code the round to nearest 5 minutes

`````` -(NSDate *)roundDateTo5Minutes:(NSDate *)mydate{
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:mydate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// if less then 3 then round down
if (remain<3){
// Subtract the remainder of time to the date to round it down evenly
}else{
// Add the remainder of time to the date to round it up evenly
}
return mydate;
}
``````
Score: 6

Most replies here are unfortunately not 19 perfectly correct (even though they seem 18 to work quite well for most users), as they 17 either rely on the current active system 16 calendar to be a Gregorian calendar (which 15 may not be the case) or upon the fact that 14 leap seconds don't exist and/or will always 13 be ignored by OS X an iOS. The following 12 code works copy&paste, is guaranteed 11 to be correct and it makes no such assumptions 10 (and thus will also not break in the future 9 if Apple changes leap seconds support, as 8 in that case NSCalendar will have to correctly 7 support them as well):

``````{
NSDate * date;
NSUInteger units;
NSCalendar * cal;
NSInteger minutes;
NSDateComponents * comp;

// Get current date
date = [NSDate date];

// Don't rely that `currentCalendar` is a
// Gregorian calendar that works the way we are used to.
cal = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar
];
[cal autorelease]; // Delete that line if using ARC

// Units for the day
units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
// Units for the time (seconds are irrelevant)
units |= NSHourCalendarUnit | NSMinuteCalendarUnit;

// Split current date into components
comp = [cal components:units fromDate:date];

// Get the minutes,
// will be a number between 0 and 59.
minutes = [comp minute];
// Unless it is a multiple of 5...
if (minutes % 5) {
// ... round up to the nearest multiple of 5.
minutes = ((minutes / 5) + 1) * 5;
}

// Set minutes again.
// Minutes may now be a value between 0 and 60,
// but don't worry, NSCalendar knows how to treat overflows!
[comp setMinute:minutes];

// Convert back to date
date = [cal dateFromComponents:comp];
}
``````

If the current time 6 is already a multiple of 5 minutes, the 5 code will not change it. The original question 4 did not specify this case explicitly. If 3 the code shall always round up to the next 2 multiple of 5 minutes, just remove the test 1 `if (minutes % 5) {` and it will always round up.

Score: 4

The answer from @ipje did the trick for the next 6 5 minutes but I needed something more flexible 5 and I wanted to get rid of all the magic 4 numbers. I found a solution thanks to an 3 answer to a similar question My solution uses the Swift 5.2 and `Measurement` to 2 avoid using magic numbers:

``````extension UnitDuration {
var upperUnit: Calendar.Component? {
if self == .nanoseconds {
return .second
}

if self == .seconds {
return .minute
}
if self == .minutes {
return .hour
}
if self == .hours {
return .day
}
return nil
}
}
extension Date {
func roundDate(to value: Int, in unit: UnitDuration, using rule: FloatingPointRoundingRule, and calendar: Calendar = Calendar.current) -> Date? {
guard unit != .picoseconds && unit != .nanoseconds,
let upperUnit = unit.upperUnit else { return nil }
let value = Double(value)
let unitMeasurement = Measurement(value: value, unit: unit)
let interval = unitMeasurement.converted(to: .seconds).value

let startOfPeriod = calendar.dateInterval(of: upperUnit, for: self)!.start
var seconds = self.timeIntervalSince(startOfPeriod)
seconds = (seconds / interval).rounded(rule) * interval
}

func roundDate(toNearest value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .toNearestOrEven)
}

func roundDate(toNext value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .up)
}
}

``````

In my playground 1 :

``````let calendar = Calendar.current
let date = Calendar.current.date(from: DateComponents(timeZone: TimeZone.current, year: 2020, month: 6, day: 12, hour: 00, minute: 24, second: 17, nanosecond: 577881))! // 12 Jun 2020 at 00:24

var roundedDate = date.roundDate(toNext: 5, in: .seconds)!
//"12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 20 nanosecond: 0 isLeapMonth: false

roundedDate = date.roundDate(toNearest: 5, in: .seconds)!
// "12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 15 nanosecond: 0 isLeapMonth: false

roundedDate = date.roundDate(toNext: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false

roundedDate = date.roundDate(toNearest: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false

roundedDate = date.roundDate(toNext: 5, in: .hours)!
// "12 Jun 2020 at 05:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 5 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false

roundedDate = date.roundDate(toNearest: 5, in: .hours)!
// "12 Jun 2020 at 00:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false

``````
Score: 3

I just started experimenting with this for 6 an app of mine, and came up with the following. It 5 is in Swift, but the concept should be understandable 4 enough, even if you don't know Swift.

``````func skipToNextEvenFiveMinutesFromDate(date: NSDate) -> NSDate {
var componentMask : NSCalendarUnit = (NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute)
var components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)

components.minute += 5 - components.minute % 5
components.second = 0
if (components.minute == 0) {
components.hour += 1
}

return NSCalendar.currentCalendar().dateFromComponents(components)!
}
``````

The 3 result looks correct in my playground, where 2 I inject various custom dates, close to 1 midnight, close to a new year etc.

Edit: Swift2 support:

`````` func skipToNextEvenFiveMinutesFromDate(date: NSDate) -> NSDate {
let componentMask : NSCalendarUnit = ([NSCalendarUnit.Year , NSCalendarUnit.Month , NSCalendarUnit.Day , NSCalendarUnit.Hour ,NSCalendarUnit.Minute])
let components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)

components.minute += 5 - components.minute % 5
components.second = 0
if (components.minute == 0) {
components.hour += 1
}

return NSCalendar.currentCalendar().dateFromComponents(components)!
}
``````
Score: 2

Here's my solution to the original problem 1 (rounding up) using ayianni's wrapper idea.

``````-(NSDate *)roundDateToCeiling5Minutes:(NSDate *)mydate{
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:mydate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// Add the remainder of time to the date to round it up evenly
return mydate;
}
``````
Score: 2

I know this is an older thread, but since 13 there are more recent answers I will share 12 the utility method that I use to round an 11 NSDate to the nearest 5 minute interval.

I 10 use this to populate a UITextField with 9 the current UIDatePicker date when it becomes 8 FirstResponder. You can't just use [NSDate 7 date] when the UIDatePicker is configured 6 with something other than a 1 minute interval. Mine 5 are configured with 5 minute intervals.

``````+ (NSDate *)roundToNearest5MinuteInterval {

NSDate *ceilingDate = [NSDate dateWithTimeIntervalSinceReferenceDate:ceil([[NSDate date] timeIntervalSinceReferenceDate]/300.0)*300.0];
NSDate *floorDate = [NSDate dateWithTimeIntervalSinceReferenceDate:floor([[NSDate date] timeIntervalSinceReferenceDate]/300.0)*300.0];
NSTimeInterval ceilingInterval = [ceilingDate timeIntervalSinceNow];
NSTimeInterval floorInterval = [floorDate timeIntervalSinceNow];

if (fabs(ceilingInterval) < fabs(floorInterval)) {
return ceilingDate;
} else {
return floorDate;
}
}
``````

Ignoring 4 the title of the question and reading what 3 @aler really wants to accomplish (rounding 2 UP to the nearest 5 minute). All you have 1 to do is the following:

``````NSDate *ceilingDate = [NSDate dateWithTimeIntervalSinceReferenceDate:ceil([[NSDate date] timeIntervalSinceReferenceDate]/300.0)*300.0];
``````
Score: 2

One more Swift generic solution, which works 1 up to 30 minutes rounding using NSCalendar

``````extension NSDate {
func nearest(minutes: Int) -> NSDate {
assert(minutes <= 30, "nearest(m) suppport rounding up to 30 minutes");
let cal = NSCalendar.currentCalendar();
var time = cal.components(.CalendarUnitMinute | .CalendarUnitSecond, fromDate: self);
let rem = time.minute % minutes
if rem > 0 {
time.minute = minutes - rem;
}
time.second = -time.second;
time.nanosecond = -time.nanosecond //updated 7.07.15
let date = cal.dateByAddingComponents(time, toDate: self, options: NSCalendarOptions(0));
return date!;
}
}
``````
Score: 1

Had been looking for this myself, but using 5 the example above gave me from year 0001 4 dates.

Here's my alternative, incorporated 3 with smorgan's more elegant mod suggestion 2 though beware I haven't leak tested this 1 yet:

``````NSDate *myDate = [NSDate date];
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:myDate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// Add the remainder of time to the date to round it up evenly
``````
Score: 1

I rewrote @J3RM 's solution as an extension 3 in Swift on the NSDate class. Here it is 2 for rounding a date to the nearest 15th 1 minute interval:

``````extension NSDate
{
func nearestFifteenthMinute() -> NSDate!
{
let referenceTimeInterval = Int(self.timeIntervalSinceReferenceDate)
let remainingSeconds = referenceTimeInterval % 900
var timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds
if remainingSeconds > 450
{
timeRoundedTo5Minutes = referenceTimeInterval + (900 - remainingSeconds)
}
let roundedDate = NSDate.dateWithTimeIntervalSinceReferenceDate(NSTimeInterval(timeRoundedTo5Minutes))
return roundedDate
}
}
``````
Score: 0

I'm not sure how efficient NSDateComponents 6 are, but if you just want to deal with the 5 NSDate itself it can give you values based 4 on seconds which can then be manipulated.

For 3 example, this method rounds down to the 2 nearest minute. Change the 60 to 300 and 1 it will round down to nearest 5 minutes.

``````+ (NSDate *)dateRoundedDownToMinutes:(NSDate *)date {
// Strip miliseconds by converting to int
int referenceTimeInterval = (int)[date timeIntervalSinceReferenceDate];

int remainingSeconds = referenceTimeInterval % 60;
int timeRoundedDownToMinutes = referenceTimeInterval - remainingSeconds;

NSDate *roundedDownDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedDownToMinutes];

return roundedDownDate;
}
``````
Score: 0

This is a generic solution which rounds 1 up to the nearest input 'mins':

``````+(NSDate *)roundUpDate:(NSDate *)aDate toNearestMins:(NSInteger)mins
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSUIntegerMax fromDate:aDate];

NSInteger dateMins = components.minute;
dateMins = ((dateMins+mins)/mins)*mins;

[components setMinute:dateMins];
[components setSecond:0];
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
``````
Score: 0
``````- (NSDate *)roundDateToNearestFiveMinutes:(NSDate *)date
{
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:date];
NSInteger minutes = [time minute];
float minuteUnit = ceil((float) minutes / 5.0);
minutes = minuteUnit * 5.0;
[time setMinute: minutes];
return [[NSCalendar currentCalendar] dateFromComponents:time];
}
``````

0

More Related questions