我必须在当地时间下午6点设置一个UIAlertNotification。时间定在上午11点,到下午6点,因为我现在所在的当地时区是GMT+6:00。然而,在不同的时区,情况会有所不同。我要在任何时区下午6点。有人能帮帮我吗?谢谢。
NSDateComponents *comps=[[[NSDateComponents alloc]init]autorelease];
[comps setYear:[year intValue]];
[comps setMonth:[month intValue]];
[comps setDay:[day intValue]];
[comps setHour:[@"11" intValue]];//6pm
[comps setMinute:0];
[comps setMinute:0];
[comps setTimeZone:[NSTimeZone localTimeZone]];
NSCalendar *cal=[[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date1=[[cal dateFromComponents:comps]retain];//本地警报代码
NSInteger minutesAhead = 0;
NSTimeInterval seconds = (60 * minutesAhead) * -1;
NSDate *actualFireDate = [date1 dateByAddingTimeInterval:seconds];
NSMutableDictionary *dictC = [NSMutableDictionary dictionaryWithDictionary:userInfo];
[dictC setObject:date1 forKey:@"PCOriginalReminderDate"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = actualFireDate;
notification.alertBody = message;
notification.userInfo = dictC;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];备注:
当我打印日期时,上面写着:
date = "2012-04-04 18:00:00 +0000";我认为这意味着上午11点是格林尼治时间,并增加了7个小时(从格林尼治时间+7 -白天开始),并使之达到下午18点。
发布于 2012-03-22 00:05:44
您想要设置您的timeZone性质的UILocalNotification
fireDate中指定的日期将根据此属性的值进行解释。如果您指定了“零”(默认值),那么火日期将被解释为一个绝对的格林尼治时间,这适用于诸如倒计时计时器之类的情况。如果将有效的NSTimeZone对象分配给此属性,则将火日期解释为在时区发生更改时自动调整的挂钟时间;适合这种情况的示例是闹钟。
因此,如果您将通知的fireDate设置为在代码段中创建的时间,并将其timeZone设置为[NSTimeZone localTimeZone],那么您就可以继续了。
https://stackoverflow.com/questions/9814526
复制相似问题