3 回答

TA貢獻1839條經驗 獲得超15個贊
讓我們假設兩個日期:
NSDate *date1;
NSDate *date2;
然后,下面的比較將確定哪個是較早/較晚/相同:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}
請參閱NSDate類文檔以獲取更多詳細信息。

TA貢獻1825條經驗 獲得超6個贊
晚了,但是比較NSDate對象的另一種簡單方法是將它們轉換為原始類型,從而可以輕松使用'>''<''=='等
例如。
if ([dateA timeIntervalSinceReferenceDate] > [dateB timeIntervalSinceReferenceDate]) {
//do stuff
}
timeIntervalSinceReferenceDate將日期轉換為自參考日期(格林尼治標準時間2001年1月1日)以來的秒數。當timeIntervalSinceReferenceDate返回NSTimeInterval(它是一個double typedef)時,我們可以使用原始比較器。
- 3 回答
- 0 關注
- 665 瀏覽
添加回答
舉報