比较NSDate今天或昨天 [英] Compare NSDate for Today or Yesterday

查看:90
本文介绍了比较NSDate今天或昨天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Well I guess this has been asked a thousand times, but for some reason the answeres dont really work or had other problems,....

无论如何这里是我有什么工作:

Anyway here is what I have "working" :

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

现在问题:

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

That seems to be an awefull lot of code for just a date comparinson, is there an easier way ?

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

And the most important question is the release of all the objects. As might have guessed, I use this in a UITableViewController. I also have these lines in my code :

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

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

The problem is that as soon that I uncomment one of these lines, my app crashes and I have no idea why ?! I hope someone can enlighten me here.

非常感谢。

推荐答案

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

The easiest way to do it is to just compare the description of the dates:

// 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:而不是。

If you want to change the format of the date string you could use descriptionWithLocale: instead.

这篇关于比较NSDate今天或昨天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆