第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

比較兩個(gè)NSDate并忽略時(shí)間分量

比較兩個(gè)NSDate并忽略時(shí)間分量

DIEA 2019-12-17 15:20:25
比較兩個(gè)NSDate的最有效/推薦的方法是什么?我希望能夠看到兩個(gè)日期是否都在同一天,與時(shí)間無關(guān),并且已經(jīng)開始編寫一些代碼,該代碼使用NSDate類中的timeIntervalSinceDate:方法,并獲取該值的整數(shù)除以秒數(shù)一天內(nèi)。這似乎很漫長(zhǎng),我覺得我缺少明顯的東西。我要修復(fù)的代碼是:if (!([key compare:todaysDate] == NSOrderedDescending)){    todaysDateSection = [eventSectionsArray count] - 1;}其中key和todaysDate是NSDate對(duì)象,而todaysDate使用以下方法創(chuàng)建:NSDate *todaysDate = [[NSDate alloc] init];
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊

在進(jìn)行比較之前,您將日期中的時(shí)間設(shè)置為00:00:00:


unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

NSCalendar* calendar = [NSCalendar currentCalendar];


NSDateComponents* components = [calendar components:flags fromDate:date];


NSDate* dateOnly = [calendar dateFromComponents:components];


// ... necessary cleanup


查看完整回答
反對(duì) 回復(fù) 2019-12-17
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

令我驚訝的是,沒有其他答案可以使用此選項(xiàng)來獲取對(duì)象的“開始日期”日期:


[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay startDate:&date1 interval:NULL forDate:date1];

[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay startDate:&date2 interval:NULL forDate:date2];

哪個(gè)設(shè)置date1并date2開始各自的日子。如果它們相等,則它們?cè)谕惶臁?/p>


或此選項(xiàng):


NSUInteger day1 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit: forDate:date1];

NSUInteger day2 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:date2];

其中規(guī)定day1,并day2以可以比較有點(diǎn)武斷值。如果它們相等,則它們?cè)谕惶臁?/p>


查看完整回答
反對(duì) 回復(fù) 2019-12-17
?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

iOS 8向NSCalendar引入了一種新方法,使此操作變得更加容易。


- (NSComparisonResult)compareDate:(NSDate *)date1 toDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);

您可以將粒度設(shè)置為重要的單位。這將忽略所有其他單位,并將比較范圍限制為所選單位。


查看完整回答
反對(duì) 回復(fù) 2019-12-17
  • 3 回答
  • 0 關(guān)注
  • 848 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)