gpt4 book ai didi

objective-c - 关于 NSDateComponents 时区

转载 作者:行者123 更新时间:2023-11-29 04:44:45 25 4
gpt4 key购买 nike

我必须在本地时间下午 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 点是格林尼治标准时间 (GMT),加上 7 个小时(自格林尼治标准时间 +7 - 日光起),就变成了晚上 18 点。

最佳答案

您想要设置 timeZone property您的UILocalNotification:

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

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

关于objective-c - 关于 NSDateComponents 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com