3 回答
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(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");
}
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
晚了,但是比較NSDate對(duì)象的另一種簡(jiǎn)單方法是將它們轉(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í),我們可以使用原始比較器。
添加回答
舉報(bào)
