If / Else语句为Date..iPhone SDK

时间:2010-08-14 22:30:35

标签: iphone

我这里有一些代码。如何编写if / else语句来确定itemDate是否已经通过?因此,如果itemDate已经通过,那么localnotif.fireDate应该是它现在的,加上86400(24小时),否则保持原样。

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:item.day];
        [dateComps setMonth:item.month];
        [dateComps setYear:item.year];
        [dateComps setHour:item.hour];
        [dateComps setMinute:item.minute];
        NSDate *itemDate = [calendar dateFromComponents:dateComps];
        [dateComps release];
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;

        localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)] ;
            NSLog(@"fireDate is %@",localNotif.fireDate);

        localNotif.timeZone = [NSTimeZone defaultTimeZone];

1 个答案:

答案 0 :(得分:2)

  1. 您可以使用[NSDate date]
  2. 检索当前日期
  3. NSDate有一个名为laterDate:的非常方便的方法,可以将日期与接收者进行比较,并返回以后的日期。
  4. 您可以使用isEqual:
  5. 查看两个日期是否相等

    换句话说:

    NSDate * notificationDate = [localNotif fireDate];
    NSDate * now = [NSDate date];
    
    if ([[now laterDate:notificationDate] isEqual:now]) {
      //in a comparison between "now" and "notificationDate", "now" is the later date,
      //meaning "notificationDate" has already passed
      notificationDate = [notificationDate dateByAddingTimeInterval:86400];
    }
    [localNotif setFireDate:notificationDate];