0

我必须在当地时间下午 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 点。

4

1 回答 1

2

你想设置你的timeZone属性UILocalNotification

fireDate 中指定的日期将根据此属性的值进行解释。如果您指定 nil(默认值),则触发日期被解释为绝对 GMT 时间,这适用于倒计时计时器等情况。如果您为该属性分配一个有效的 NSTimeZone 对象,则触发日期将被解释为挂钟时间,当时区发生变化时会自动调整该时间;适合这种情况的一个例子是闹钟。

因此,如果您将通知设置fireDate为您在代码段中创建的时间,并将其设置为timeZone[NSTimeZone localTimeZone]那么您就可以开始了。

于 2012-03-22T00:05:44.450 回答