3 回答
TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
讓我們假設(shè)兩個(gè)日期:
NSDate *date1;
NSDate *date2;
然后,下面的比較將確定哪個(gè)是較早/較晚/相同:
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");
}
請(qǐng)參閱NSDate類文檔以獲取更多詳細(xì)信息。
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
晚了,但是比較NSDate對(duì)象的另一種簡單方法是將它們轉(zhuǎn)換為原始類型,從而可以輕松使用'>''<''=='等
例如。
if ([dateA timeIntervalSinceReferenceDate] > [dateB timeIntervalSinceReferenceDate]) {
//do stuff
}
timeIntervalSinceReferenceDate將日期轉(zhuǎn)換為自參考日期(格林尼治標(biāo)準(zhǔn)時(shí)間2001年1月1日)以來的秒數(shù)。當(dāng)timeIntervalSinceReferenceDate返回NSTimeInterval(它是一個(gè)double typedef)時(shí),我們可以使用原始比較器。
- 3 回答
- 0 關(guān)注
- 685 瀏覽
添加回答
舉報(bào)
