我有Here
生成的时间戳//Timestamp 1473163200000
我想根据当前时区将其转换为日期对象。
我这样做:
NSDate *gmtDate = [NSDate dateWithTimeIntervalSince1970:1473163200000/1000];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];
NSDate *localDate = [gmtDate dateByAddingTimeInterval:timeZoneOffset];
现在当我po
当地日期时,我得到了这个:
(lldb) po localDate
2016-09-06 17:30:00 +0000
当我使用DateFormatter记录localDate
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSString *newDate = [dateFormat stringFromDate:localDate];
我得到了这个:
(lldb) po newDate
2016-09-06-23-00-00
但我希望这是:
Tue Sep 06 2016 17:30:00
我缺少什么? 请避免格式化我对时区更加好奇。
答案 0 :(得分:0)
试试此代码
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1473163200000/1000];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE MMM dd yyyy HH:mm:ss";
NSString *formattedDate = [dateFormatter stringFromDate: date];
NSLog(@"formattedDate: %@",formattedDate);
输出:
formattedDate: Tue Sep 06 2016 17:30:00