如何比较可可的两个日期(仅限日期;不是时间)?

时间:2022-05-21 14:18:52

Basically, I want to figure out if it's the next day. So, I'm storing the current date (e.g. Jan 2) constantly in a plist. But the next time the user opens the application, if the date has changed (e.g. Jan 3), I want to do something. Note that a simple ascending order check wouldn't work because I don't want to know if one date is later than another date, if the difference is only in hours. I need to be able to differentiate Jan 2 11:50 and Jan 3 2:34 but not Jan 3 2:34 and Jan 3 5:12.

基本上,我想弄清楚它是否是第二天。因此,我将当前日期(例如1月2日)不断存储在plist中。但是下次用户打开应用程序时,如果日期已经改变(例如1月3日),我想做点什么。请注意,简单的升序检查不起作用,因为我不想知道一个日期是否晚于另一个日期,如果差异仅以小时为单位。我需要能够区分1月2日11:50和1月3日2:34而不是1月3日2:34和1月5日5:12。

4 个解决方案

#1


24  

I use the following that I found SO:

我使用以下内容找到了SO:

- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
    NSCalendar* calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

#2


0  

Looking at the Date Time Programming Guide gives you an example of how to calculate the difference between two dates as by component: for example:

查看日期时间编程指南为您提供了如何按组件计算两个日期之间差异的示例:例如:

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc] 
    initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags 
    fromDate:startDate 
    toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

#3


-1  

My version based off Neils' (warning: untested):

我的版本基于Neils'(警告:未经测试):

Mine is more similar to - (NSComparisonResult)compare: on NSDate. To check for days being equal, do [myDate compareDayMonthAndYear:otherDate] == NSOrderedSame

我的更类似于 - (NSComparisonResult)比较:在NSDate上。要检查天数相等,请[myDate compareDayMonthAndYear:otherDate] == NSOrderedSame

- (NSComparisonResult)compareDayMonthAndYear:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *selfComponents = [calendar components:unitFlags fromDate:self];
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

    BOOL dayIsEqual = (selfComponents.day == dateComponents.day && selfComponents.month == dateComponents.month && selfComponents.year == dateComponents.year);
    if (dayIsEqual) {
        return NSOrderedSame;
    }
    return [self compare:date];
}

#4


-2  

Use NSCalendarDate instead of NSDate, and compare what they return from -dayOfCommonEra

使用NSCalendarDate代替NSDate,并比较它们从-dayOfCommonEra返回的内容

@implementation NSDate (sameDayOrNot)

- (BOOL) sameDayAsDate:(NSDate *) otherDate
{
NSCalendarDate *date1 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:self];
NSCalendarDate *date2 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:otherDate];
BOOL result = ([date1 dayOfCommonEra] == [date2 dayOfCommonEra]);
[date1 release];
[date2 release];
return result;
}

@end

#1


24  

I use the following that I found SO:

我使用以下内容找到了SO:

- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
    NSCalendar* calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

#2


0  

Looking at the Date Time Programming Guide gives you an example of how to calculate the difference between two dates as by component: for example:

查看日期时间编程指南为您提供了如何按组件计算两个日期之间差异的示例:例如:

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc] 
    initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags 
    fromDate:startDate 
    toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

#3


-1  

My version based off Neils' (warning: untested):

我的版本基于Neils'(警告:未经测试):

Mine is more similar to - (NSComparisonResult)compare: on NSDate. To check for days being equal, do [myDate compareDayMonthAndYear:otherDate] == NSOrderedSame

我的更类似于 - (NSComparisonResult)比较:在NSDate上。要检查天数相等,请[myDate compareDayMonthAndYear:otherDate] == NSOrderedSame

- (NSComparisonResult)compareDayMonthAndYear:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *selfComponents = [calendar components:unitFlags fromDate:self];
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

    BOOL dayIsEqual = (selfComponents.day == dateComponents.day && selfComponents.month == dateComponents.month && selfComponents.year == dateComponents.year);
    if (dayIsEqual) {
        return NSOrderedSame;
    }
    return [self compare:date];
}

#4


-2  

Use NSCalendarDate instead of NSDate, and compare what they return from -dayOfCommonEra

使用NSCalendarDate代替NSDate,并比较它们从-dayOfCommonEra返回的内容

@implementation NSDate (sameDayOrNot)

- (BOOL) sameDayAsDate:(NSDate *) otherDate
{
NSCalendarDate *date1 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:self];
NSCalendarDate *date2 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:otherDate];
BOOL result = ([date1 dayOfCommonEra] == [date2 dayOfCommonEra]);
[date1 release];
[date2 release];
return result;
}

@end