简体   繁体   中英

How to know if two NSDate are in the same day

Do you know how to know if two NSDate are the same day. I want to take into account the locale...

It could be easy to use a timeIntervalSinceDate: but Monday 23H58 and Tuesday 00H01 are not in the same day...

Dealing with NSDate and locale for calculation is not very easy.

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])

etc.

I don't know if isEquals would do what you want on NSDateComponents .

Since iOS 8 this is straightforward:

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

and it becomes a little simpler yet with Swift 3 :

let isSameDay = Calendar.current.isDate(date1, inSameDayAs: date2)

Use NSCalendar and NSDateComponents for that:

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]);

Swift:

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)
}

Do it with 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];

If your app is going to running in OS X 10.9+ or iOS 8+ only, you can use the new APIs of NSCalender .

/*
    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);

There are also a lot of convenient APIs. However, Apple hasn't mentioned them in their doc set. Anyway, the APIs are public and you can use them.

Set timeZone if the date is not system locale.

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: 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM