2012-03-21 65 views
0

我必须在当地时间下午6点设置UIAlertNotification。时间设定在上午11点,因为我现在所处的当地时区是格林威治标准时间+6:00,因此时间为下午18点。但是,在不同的时区它会有所不同。我希望在任何时区下午六点。任何人都可以帮助我吗?谢谢。关于NSDateComponents TimeZone

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点是GMT和通过增加7小时(自格林威治标准时间+7 - 日光以来),并使其18点。

回答

1

你要设置你的UILocalNotificationtimeZone property

在fireDate指定的日期为根据此属性的值解释。如果您指定nil(默认值),则会将启动日期解释为绝对GMT时间,适用于倒数计时器等情况。如果您为此属性指定了有效的NSTimeZone对象,则会将启用日期解释为在时区发生更改时自动调整的挂钟时间;适合这种情况的例子是闹钟。

因此,如果您设置通知的fireDate你在你的代码段创建,并设置其timeZone[NSTimeZone localTimeZone]那么你会好起来的时间。

+0

我想我做的一切都是对的。我已添加完整的代码。请让我知道我犯了什么错误。 – Ahsan 2012-03-22 00:44:58

+0

当你说“当我打印日期”时,你在说什么日期?另外,你没有设置通知的'timeZone'属性。 – yuji 2012-03-22 08:06:58