2011-04-29 106 views
7

你知道如何知道兩個NSDate是同一天嗎?我要考慮到區域...如何知道兩個NSDate是否在同一天

它可以很容易使用timeIntervalSinceDate:但週一23H58和週二00H01不在同一天...

NSDate和語言環境進行計算處理是不是很容易。

回答

18
NSCalendar *calendar = [NSCalendar currentCalendar]; 

NSDateComponents *componentsForFirstDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:firstDate]; 

NSDateComponents *componentsForSecondDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:secondDate]; 

if ([componentsForFirstDate year] == [componentsForSecondDate year]) 

我不知道是否會isEquals你想上什麼NSDateComponents

+0

[NSCalendar currentCalendar]是十分緩慢的,所以如果可能的緩存它。 – 2011-04-29 15:38:00

+0

感謝您的回答。 「緩存」currentCal是什麼意思?我把它放在一個單身人士? – iwalktheline 2011-04-29 16:08:33

+0

或者在NSCalendar上創建一個類別,將currentCalendar的結果存儲爲一個靜態變量。 – 2011-04-29 19:49:12

7

使用NSCalendar和NSDateComponents爲:

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *comps1 = [cal components:(NSMonthCalendarUnit| NSYearCalendarUnit | NSDayCalendarUnit) 
             fromDate:date1]; 
NSDateComponents *comps2 = [cal components:(NSMonthCalendarUnit| NSYearCalendarUnit | NSDayCalendarUnit) 
             fromDate:date2]; 


BOOL sameDay = ([comps1 day] == [comps2 day] 
        && [comps1 month] == [comps2 month] 
        && [comps1 year] == [comps2 year]); 
5

與NSDateFormatter開始:

NSDateFormatter *_df = [[NSDateFormatter alloc] init]; 
_df.dateFormat = @"yyyy.MM.dd"; 
NSString *_d1 = [_df stringFromDate:_date1]; 
NSString *_d2 = [_df stringFromDate:_date2]; 
if ([_d1 isEqualToString:_d2] == YES) 
{ 
    // d1 and d2 is on same day/month/year 
} 
[_df release]; 
0

設置的timeZone如果日期是不是系統區域設置。

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:self.cityData.timeZone]; 
[calendar setTimeZone:timeZone]; 
NSDateComponents *componentsForFirstDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:weatherDataDate]; 
NSDateComponents *componentsForSecondDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:yesterday];    
if ([componentsForFirstDate day] == [componentsForSecondDate day]) { 
    // TODO: 
} 
6

斯威夫特:

func isSameDays(date1:NSDate, _ date2:NSDate) -> Bool { 
    let calendar = NSCalendar.currentCalendar() 
    var comps1 = calendar.components([NSCalendarUnit.Month , NSCalendarUnit.Year , NSCalendarUnit.Day], fromDate:date1) 
    var comps2 = calendar.components([NSCalendarUnit.Month , NSCalendarUnit.Year , NSCalendarUnit.Day], fromDate:date2) 

    return (comps1.day == comps2.day) && (comps1.month == comps2.month) && (comps1.year == comps2.year) 
} 
1

如果您的應用程序將會在OS X 10.9+或iOS上運行8+只有,你可以使用NSCalender新的API。

/* 
    This API compares the given dates down to the given unit, reporting them equal if they are the same in the given unit and all larger units. 
*/ 
- (BOOL)isDate:(NSDate *)date1 equalToDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0); 

還有很多方便的API。但是,蘋果並沒有在他們的文檔中提到它們。無論如何,這些API是公開的,您可以使用它們。

10

由於iOS 8的這個很簡單:

let isSameDay = NSCalendar.currentCalendar().isDate(date1, inSameDayAsDate: date2) 

並且變得稍微簡單但與夫特3

let isSameDay = Calendar.current.isDate(date1, inSameDayAs: date2) 
相關問題