如何知道两个NSDate是否在同一天

时间:2011-04-29 15:22:49

标签: iphone cocoa nsdate

您知道如何知道两个NSDate是否在同一天。我想考虑一下语言环境......

使用timeIntervalSinceDate:可能很容易,但星期一23H58和星期二00H01不在同一天......

处理NSDate和语言环境进行计算并不容易。

7 个答案:

答案 0 :(得分: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上执行您想要的操作。

答案 1 :(得分:10)

从iOS 8开始,这很简单:

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

使用 Swift 3

,它变得更简单了
let isSameDay = Calendar.current.isDate(date1, inSameDayAs: date2)

答案 2 :(得分: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]);

答案 3 :(得分: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)
}

答案 4 :(得分: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];

答案 5 :(得分: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。但是,Apple没有在他们的文档集中提到它们。无论如何,API是公开的,您可以使用它们。

答案 6 :(得分: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: 
}