如何在iPhone中获取过去和未来日期的值

时间:2011-07-11 13:01:54

标签: iphone nsdate

我正在制作一个appplication,其中我从服务器获得一些事件响应,如果有一些日期与该事件相关,我需要知道如何区分这两个日期,在我的应用程序中有两个分段栏,第一个是即将发生的事件,第二个是过去的事件,现在我需要以这种格式显示与日期相关的事件,意味着事件接下来要进入,需要在即将发生的事件中显示以及已经消失的事件需要在过去的事件中显示,所有日期都来自web服务器,格式为dd / mm /年,我已将所有日期存储在数组中并将其显示在表格中,但如何区分即将到来的日期和过去的日期。

2 个答案:

答案 0 :(得分:0)

NSDate *firstDate = ...
NSDate *secondDate = ...

NSDate *myDate = [NSDate date];

switch ([myDate compare:firstDate]) {
    case NSOrderedAscending:
        NSLog(@"myDate is older");
        // do something
        break;
    case NSOrderedSame:
        NSLog(@"myDate is the same as firstDate");
        // do something
        break;
    case NSOrderedDescending:
        NSLog(@"myDate is more recent");
        // do something
        break;
}

switch ([myDate compare:secondDate]) {
    case NSOrderedAscending:
        NSLog(@"myDate is older");
        // do something
        break;
    case NSOrderedSame:
        NSLog(@"myDate is the same as secondDate");
        // do something
        break;
    case NSOrderedDescending:
        NSLog(@"myDate is more recent");
        // do something
        break;
}

答案 1 :(得分:0)

请阅读“Date and Time Programming Guide”。这是您所有问题的绝佳资源。我强烈建议您使用“Creating a Date from Components”和“Calendrical Calculations”章节。