我必须从现在到第二个星期二,第三个星期五安排本地通知等。所以请说明我如何获得FireDate。任何帮助将受到高度赞赏。
答案 0 :(得分:0)
以下是如何使用NSDateComponents
和NSCalendar
在下周的周二。我希望这有帮助
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear fromDate:now];
components.weekday = 3; //Tuesday on Gregorian calendar
components.weekOfYear += 1; //next week
NSDate *endDate = [calendar dateFromComponents:components];
endDate
这里是您本地通知的fireDate
。有关此问题的进一步帮助,请阅读NSDateComponents
文档。祝你好运!
答案 1 :(得分:0)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *c = [[NSDateComponents alloc]init];
[c setYear:2012];
[c setWeekday:3]; //1 for Sunday 2 for monday 3 for Tuesday
[c setWeekdayOrdinal:2]; // The 2rd Tuesday
for (int i = 1; i <= 12; i++) {
[c setMonth:i];
NSDate *date = [gregorian dateFromComponents:c];
NSLog(@"Date[%i]: %@",i,date);
}
从此您将获得每个月的星期二日期。然后,您可以在1小时后显示通知时间的本地通知,因此请使用
NSDate *firedate = [NSDate dateWithTimeInterval:1*60*60 sinceDate:tuesdayDate];