将时间戳转换为本地时区日期时出现问题
Issue while converting timeStamp to local Time Zone Date
我有一个从 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
我错过了什么?
请避免格式化我对时区更好奇。
试试这个代码
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
我有一个从 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
我错过了什么? 请避免格式化我对时区更好奇。
试试这个代码
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