iOS中处理日期时间,我们可以使用以下方法,请注意地区时差的问题,代码及效果如下:
//2015-09-15 18:09:28 - 1442311768
NSLog(@"2015-09-15 18:09:28");
NSDate *createTimesp =
[NSDate dateWithTimeIntervalSince1970:1442311768+28800];
NSLog(@"%@",createTimesp);
NSString *str=@"1442311768";//时间戳
NSTimeInterval time = [str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSLog(@"%@",[detaildate description]);
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:1442311768]];
NSLog(@"%@",currentDateStr);
//NSLog(@"%ld",(long)[[NSDate date] timeIntervalSince1970]);
运行效果:
2015-09-16 11:14:18.433 test[647:8649] 2015-09-15 18:09:28
2015-09-16 11:14:18.435 test[647:8649] 2015-09-15 18:09:28 +0000
2015-09-16 11:14:18.435 test[647:8649] 2015-09-15 18:09:28 +0000
2015-09-16 11:14:18.436 test[647:8649] 2015-09-15 18:09:28