tags:

views:

62

answers:

1

I have this bit of code here. How would I write an if/else statement to determine whether the itemDate has already passed? So if itemDate has already passed, then localnotif.fireDate should be what it is now, plus 86400 (for 24 hours) else leave as is.

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];
+2  A: 
  1. You can retrieve the current date using [NSDate date].
  2. NSDate has this really convenient method called laterDate: that compares a date with the receiver and returns which ever one is later.
  3. You can see if two dates are equal using isEqual:

In other words:

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];
Dave DeLong
Sorry, I'm not lazy or anything, but I'm extremely new to all this. I didnt write that code above, I'm only trying to fix it. How would I use laterDate?
fprime
Would this work:NSDate *laterDate = [NSDate laterDate]; if([NSDate isEqual:laterDate]) { localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60) +86400] ; NSLog(@" IFfireDate is %@",localNotif.fireDate); } else { localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)] ; NSLog(@"ELSE fireDate is %@",localNotif.fireDate); }
fprime
@mohabitar edited answer
Dave DeLong