@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
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
65