Edited at

入力された時間を5分単位に丸めるメソッド

More than 5 years have passed since last update.

@implementation NSDate (NSDateRounding)

- (NSDate *)dateByRoundingUpToFiveMinutes
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"m"];
int ref = [[formatter stringFromDate:self] intValue];//基準分を切り抜き

if(ref % 5 == 0) return self;

float def = round(ref * .1f) * 10;//0.1かけてから四捨五入して10かける
if(def < ref) def += 5;//元値より小さければ5足す
return [self dateByAddingTimeInterval:60*(def - ref)];//基準分から差分をひいて秒数に変換
}
@end