0

这是我的代码

“时间间隔”= 1372418789000;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1372418789000];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);

输出格式为DateString: 45460-03-21 10:53:20

但我需要的输出是 2013-06-28 04:26:29 America/Los_Angeles

4

2 回答 2

14

您的时间间隔以毫秒为单位,而dateWithTimeIntervalSince1970期望的时间间隔以秒为单位。将数字除以 1000 以获得正确的值:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1372418789];
// Divided by 1000 (i.e. removed three trailing zeros) ^^^^^^^^
NSString *formattedDateString = [dateFormatter stringFromDate:date];
// Fri, 28 Jun 2013 11:26:29 GMT
NSLog(@"formattedDateString: %@", formattedDateString);
于 2013-06-28T12:48:26.583 回答
0

尝试这个....

按照这个来获取NSDateFormatter

NSString *dateStr = @"1477644628477";



double timestampComment = [dateStr doubleValue]/1000;

NSTimeInterval  timeInterval =timestampComment ;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy h:m:s a"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];

NSString *formattedDateString = [dateFormatter stringFromDate:date];

NSLog(@"formattedDateString: %@", formattedDateString);
于 2016-11-02T08:10:15.210 回答