3
我使用下面的函數圓的時間間隔最近的5分鐘時間四捨五入到最接近的第10分鐘
-(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
mydate = [mydate addTimeInterval:-60*(remain)];
}else{
// Add the remainder of time to the date to round it up evenly
mydate = [mydate addTimeInterval:60*(5-remain)];
}
return mydate;
} 現在我想四捨五入的時間最接近的第十分鐘..... 任何一個可以請幫我該怎麼做的事情
謝謝。由於您發佈了(〜iOS4.0)addTimeInterval:已被折舊並被替換爲dateByAddingTimeInterval: – Dermot