使用本地时区将unix时间戳转换为NSdate

我从API请求中获取了NSDecimalNumberforms的start_timesend_times

我已经成功地将这些NSDecimalNumber s转换成NSDate s,但代码没有考虑时区。

我需要它使用设备上默认的时区。

这应该做你所需要的当前语言环境

 double unixTimeStamp =1304245000; NSTimeInterval _interval=unixTimeStamp; NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval]; NSDateFormatter *formatter= [[NSDateFormatter alloc] init]; [formatter setLocale:[NSLocale currentLocale]]; [formatter setDateFormat:@"dd.MM.yyyy"]; NSString *dateString = [formatter stringFromDate:date]; 

Unix时间没有时区 。 它在UTC中定义为1970年1月1日午夜以来的秒数。

你应该通过使用正确的时区获得NSDates

 [NSDate dateWithTimeIntervalSince1970:myEpochTimestamp]; 

尝试这样的事情

 NSDate* sourceDate = ... // your NSDate NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];