我必须在当地时间下午 6:00 设置 UIAlertNotification。时间设置为上午 11:00,将到达下午 18:00,因为我现在所在的本地时区是 GMT+6:00。但是,在不同的时区会有所不同。我希望它在任何时区都是下午 6:00。任何人都可以帮助我吗?谢谢。
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 点。