您的位置:首页 > 其它

NSDate时间戳和时间相互转化

2016-06-07 12:26 357 查看
摘要: 时间戳和时间相互转化

时间戳 -->时间

- (NSString*)CalculateTimeWithStr:(NSString*)str{

NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec

NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];

NSLog(@"date:%@",[detaildate description]);

//实例化一个NSDateFormatter对象

NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];

//设定时间格式,这里可以设置成自己需要的格式

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // EEEE -> 星期描述

y - year M- month d- day H- hour(24) h - hour(12) m - minute s - second

NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];

return currentDateStr;

}

时间-->时间戳

NSTimeInterval time = [[NSDate date] timeIntervalSince1970];

这个time就是现在时间的时间戳
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oc 时间戳