【发布时间】:2014-07-29 10:55:52
【问题描述】:
我正在使用下面的代码在 IST 下午 5 点准确地触发 localNotification。但是当记录fireDate时,它没有显示我想要的时间。我是不是哪里出错了?
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//localNotification.fireDate = myNewDate;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
//[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:11];
[dateComponents setMinute:30];
fireDate = [calendar dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *stringFromDate = [formatter stringFromDate:fireDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:stringFromDate];
NSLog(@"Fire date : %@",dateFromString);
[localNotification setFireDate:dateFromString];
我的输出: 发布日期:2014-07-29 16:48:20 +0000
【问题讨论】:
-
目标是什么——“今天下午 5 点 IST”? “下次是 IST 下午 5 点,即使那是明天”?还有什么?
标签: ios7 nsdateformatter uilocalnotification nscalendar nstimezone