7
如何比較兩個NSDates並返回它們之間的時間量。比較兩個NSDates
例如:
2010-06-20 14:29:41 -0400
2010-06-20 14:53:29 -0400
應該返回類似:23
或24
(分鐘),四捨五入秒鐘,因爲我並不真正需要的秒數。
如何比較兩個NSDates並返回它們之間的時間量。比較兩個NSDates
例如:
2010-06-20 14:29:41 -0400
2010-06-20 14:53:29 -0400
應該返回類似:23
或24
(分鐘),四捨五入秒鐘,因爲我並不真正需要的秒數。
嘗試類似:
int intervall = (int) [date1 timeIntervalSinceDate: date2]/60;
這裏完整的片段來比較兩個NSDate的,您可以根據您的查詢找到更多有用的。
NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSString *str [email protected]"12:15, 01 15 2012 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm, dd MM yy a"];
NSDate *refDate = [formatter dateFromString:str];
NSDate *today = [NSDate date];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *difference = [myCal components:unitFlags fromDate:refDate toDate:today options:0];
NSLog(@"%@",difference);
------------------------------你將得到的是-------- ---------------------------------
日曆年:0月:0日:0小時:1分鐘:19
效果很好。謝謝一堆。 – 2010-06-20 21:28:10
請注意,這可能會計算爲負數的分鐘數,具體取決於較早的日期。如果它是否定的,則取否定數字,或者使用'abs'或'fabs'函數。 – 2010-06-21 06:13:26
感謝一如既往的提示@Peter – 2010-06-21 16:52:09