我的应用程序从后端获取一些时间段并将其转换为当前时区(PST)并显示。一切工作都很完美,直到白天节省时间开始。将夏令时转换为UTC时间为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 – vadian
那样使用字面数字这是什么意思,“这种情况下的实际时间将是当天上午9点”?如果你不想做一个简单的时区转换(它会产生10 AM),请描述你想如何转换时间。 –