How to know if two NSDate are in the same day

二次信任 提交于 2019-12-06 00:11:45

问题


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.


回答1:


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.




回答2:


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)



回答3:


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



回答4:


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



回答5:


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



回答6:


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.




回答7:


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


来源:https://stackoverflow.com/questions/5833963/how-to-know-if-two-nsdate-are-in-the-same-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!