首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >目标日期前2天的NSNotification

目标日期前2天的NSNotification
EN

Stack Overflow用户
提问于 2012-08-31 11:40:47
回答 2查看 366关注 0票数 0

我想在目标date.Currently之前2天发出通知,我使用下面的代码来触发通知,但是如何在目标日期之前2天触发通知。

代码语言:javascript
复制
    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.
EN

回答 2

Stack Overflow用户

发布于 2012-09-01 11:46:53

你应该使用NSCalendar和NSDateComponents来做数学运算-类似于:

代码语言:javascript
复制
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* twoDaysAgo = [[NSDateComponents alloc] init];
twoDaysAgo.day = -2;
NSDate* dateToFire = [calendar dateByAddingComponents:twoDaysAgo toDate:date options:0];
票数 1
EN

Stack Overflow用户

发布于 2012-08-31 17:06:04

您可以对日历日期进行算术运算(年/月/日/小时...在给定时区中的表示)。尽管在大多数情况下,"24*60*60“秒是一天--但用户可能不会注意到其中的区别。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12208595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档