我这里有一些代码。如何编写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];
答案 0 :(得分:2)
[NSDate date]
。NSDate
有一个名为laterDate:
的非常方便的方法,可以将日期与接收者进行比较,并返回以后的日期。isEqual:
换句话说:
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];