首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于NSDateComponents TimeZone

关于NSDateComponents TimeZone
EN

Stack Overflow用户
提问于 2012-03-21 23:47:30
回答 1查看 2K关注 0票数 0

我必须在当地时间下午6点设置一个UIAlertNotification。时间定在上午11点,到下午6点,因为我现在所在的当地时区是GMT+6:00。然而,在不同的时区,情况会有所不同。我要在任何时区下午6点。有人能帮帮我吗?谢谢。

代码语言:javascript
复制
    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];

//本地警报代码

代码语言:javascript
复制
    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];

备注:

当我打印日期时,上面写着:

代码语言:javascript
复制
 date = "2012-04-04 18:00:00 +0000";

我认为这意味着上午11点是格林尼治时间,并增加了7个小时(从格林尼治时间+7 -白天开始),并使之达到下午18点。

EN

回答 1

Stack Overflow用户

发布于 2012-03-22 00:05:44

您想要设置您的timeZone性质UILocalNotification

fireDate中指定的日期将根据此属性的值进行解释。如果您指定了“零”(默认值),那么火日期将被解释为一个绝对的格林尼治时间,这适用于诸如倒计时计时器之类的情况。如果将有效的NSTimeZone对象分配给此属性,则将火日期解释为在时区发生更改时自动调整的挂钟时间;适合这种情况的示例是闹钟。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9814526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档