比较NSDate今天或昨天

时间:2010-05-23 22:55:03

标签: iphone uitableview nsdate

嗯,我想这已被问了一千次,但由于某些原因,答案确实没有用,或者有其他问题,......

无论如何这里是我“工作”的东西:

    NSCalendar *calendar = [NSCalendar currentCalendar];    
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    // set tomorrow (0: today, -1: yesterday)
    [comps setDay:0];
    NSDate *dateToday = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps setDay:-1];
    NSDate *dateYesterday = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps release];


NSString *todayString = [self.dateFormatter stringFromDate:dateToday] ;
NSString *yesterdayString = [self.dateFormatter stringFromDate:dateYesterday] ;
NSString *refDateString = [self.dateFormatter stringFromDate:info.date];

if ([refDateString isEqualToString:todayString]) 
{
    cell.title.text =  @"Today";
} else if ([refDateString isEqualToString:yesterdayString]) 
{
    cell.title.text =  @"Yesterday";
} else 
{
    cell.title.text =  [self.dateFormatter stringFromDate:info.date];
}

现在问题:

对于日期比较而言,这似乎是一个非常多的代码,有更简单的方法吗?

最重要的问题是所有对象的发布。可能已经猜到了,我在UITableViewController中使用它。我的代码中也有这些行:

//[calendar release];
//[currentDate release];
//[dateToday release];
//[dateYesterday release];
//[todayString release];
//[yesterdayString release];
//[refDateString release];

问题是,一旦我取消注释其中一行,我的应用程序崩溃了,我不明白为什么?!我希望有人能在这里启发我。

非常感谢。

4 个答案:

答案 0 :(得分:12)

最简单的方法是只比较日期的description

// Your dates:
NSDate * today = [NSDate date];
NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-86400]; //86400 is the seconds in a day
NSDate * refDate; // your reference date

// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * refDateString = [[refDate description] substringToIndex:10];

if ([refDateString isEqualToString:todayString]) 
{
    cell.title.text =  @"Today";
} else if ([refDateString isEqualToString:yesterdayString]) 
{
    cell.title.text =  @"Yesterday";
} else 
{
    cell.title.text =  refDateString;
}

如果您想更改日期字符串的格式,可以改用descriptionWithLocale:

答案 1 :(得分:6)

我知道这是一个古老的问题,但我自己也做了类似的事情并且遇到了它。

首先,自iOS 4.0(和Mac OS 10.6)起,NSDateFormatter可以执行相对日期,这会自动为您提供“今天”和“昨天”。

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDoesRelativeDateFormatting:YES];

NSString* dateString = [formatter stringFromDate:[NSDate date]];    
NSLog( @"date = %@", dateString );

哪个输出:

2014-03-15 15:26:37.683 TestApp[1293:303] date = Today

然而,OP正在询问如何比较今天或昨天的NSDate,我还想做的事情,除了相对日期格式。这是我提出的,在NSDate上实现的类别:

@implementation NSDate (IsToday)

- (BOOL) isToday
{
    NSCalendar* calendar = [NSCalendar currentCalendar];

    // Components representing the day of our date.
    NSDateComponents* dateComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                             fromDate:self];
    NSDate* date = [calendar dateFromComponents:dateComp];

    // Components representing today.
    NSDateComponents* todayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                              fromDate:[NSDate date]];
    NSDate* todayDate = [calendar dateFromComponents:todayComp];

    // If the dates are equal, then our date is today.
    return [date isEqualToDate:todayDate];
}

@end

我之所以这样做,是因为我可以显示今天创建的内容的时间,以及今天未创建的内容的日期 - 类似于Mail.app显示消息日期的方式。它看起来像这样:

- (NSString*) postDateToString:(NSDate*)aDate
{
    static NSDateFormatter* todayFormatter = nil;
    if( todayFormatter == nil ) {
        todayFormatter = [[NSDateFormatter alloc] init];
        [todayFormatter setTimeStyle:NSDateFormatterShortStyle];
        [todayFormatter setDateStyle:NSDateFormatterNoStyle];
    }

    static NSDateFormatter* notTodayFormatter = nil;
    if( notTodayFormatter == nil ) {
        notTodayFormatter = [[NSDateFormatter alloc] init];
        [notTodayFormatter setTimeStyle:NSDateFormatterNoStyle];
        [notTodayFormatter setDateStyle:NSDateFormatterShortStyle];
        [notTodayFormatter setDoesRelativeDateFormatting:YES];
    }

    NSDateFormatter* formatter = notTodayFormatter;

    if( [aDate isToday] ) {
        formatter = todayFormatter;
    }

    return [formatter stringFromDate:aDate];
}

答案 2 :(得分:2)

我可以建议使用这个开源项目: https://github.com/erica/NSDate-Extensions

您可以查看当前的方法:

+ (NSDate *) dateTomorrow
+ (NSDate *) dateYesterday

答案 3 :(得分:-1)

您可以使用isEqualToDate方法比较两个NSDate对象,将返回一个bool(是或否),告诉您日期是否相等。请参阅Apple Documentation

对于释放对象 - objective-c中的规则是您只释放在方法过程中为内存分配的对象。您在代码中专门为内存分配内存的唯一对象是 comps ,因此它是您应该发布的唯一对象 - 而且您已经这样做了。释放您从未分配的对象的内存可能是导致您的应用崩溃的原因。当实际分配这些对象的方法试图释放它们或对它们的另一个引用尝试访问它们时,它们会触发EXC_BAD_ACCESS错误,因为你已经释放它们了!

所以不要只是注释掉那些发布行,从脚本中删除它们 - 不需要它们!