【发布时间】:2016-06-14 08:07:46
【问题描述】:
我的应用程序从后端获取一些时隙并将其转换为当前时区 (PST) 并显示。在开始节省日光时间之前,一切都运行良好。
iOS 中有没有办法说像给我这个 UTC 的 PST 日期和时间,它会自动处理夏令时。目前它的转换很好,但仍然给我一个小时的休息时间。
这就是我正在做的事情:
-(NSDate *) toLocalTime:(NSDate*) currentDate
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"US/Pacific"];
NSInteger seconds = [tz secondsFromGMTForDate: currentDate];
return [NSDate dateWithTimeInterval: seconds sinceDate: currentDate];
}
编辑
把我的代码改成这个,结果还是一样:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *startDateString = [[self.slotsArray objectAtIndex:indexPath.row] objectForKey:@"StartDate"];
NSDate *startDate = [dateFormatter dateFromString:startDateString];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterMediumStyle;
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
NSString * dateString = [dateFormatter stringFromDate:startDate];
例如,当白天开始时,我会显示 3 月 13 日的一些时间段。在 UTC 中,我得到的时间是 17:00,相当于我们今天的 10 点。但在这种情况下,实际时间是当天上午 9 点。我该如何解决?
【问题讨论】:
-
基本上使用
NSCalendar和NSDateComponents进行所有 日历计算,而不是使用像86400 这样的文字数字 -
“在这种情况下,实际时间是当天上午 9 点”是什么意思?如果您不想进行简单的时区转换(产生上午 10 点),请描述您希望如何转换时间。
标签: ios objective-c nsdate dst nstimezone