我想在目标date.Currently之前2天发出通知,我使用下面的代码来触发通知,但是如何在目标日期之前2天触发通知。
NSString *frstdate = [[calenderarray objectAtIndex:k] objectForKey:@"date"];
NSLog(@"frstdate..%@",frstdate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:frstdate];
NSLog(@"date..%@",date);
NSDate *dateToFire = [[[NSDate alloc] initWithTimeInterval:-24*60*60 sinceDate:date]autorelease];
NSLog(@"dateToFire..%@",dateToFire);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Get Food"], @"foodItem", nil] ;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Thanks in advance.发布于 2012-09-01 11:46:53
你应该使用NSCalendar和NSDateComponents来做数学运算-类似于:
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* twoDaysAgo = [[NSDateComponents alloc] init];
twoDaysAgo.day = -2;
NSDate* dateToFire = [calendar dateByAddingComponents:twoDaysAgo toDate:date options:0];发布于 2012-08-31 17:06:04
您可以对日历日期进行算术运算(年/月/日/小时...在给定时区中的表示)。尽管在大多数情况下,"24*60*60“秒是一天--但用户可能不会注意到其中的区别。
https://stackoverflow.com/questions/12208595
复制相似问题