iOS怎么实现日期和时间戳的相互转换
2016-11-02 14:01
363 查看
在项目开发中我们经常需要把日期和时间戳相互转换代码如下:
日期转换成时间戳:
currentYear=@"2016"
NSDateFormatter *formatter = [[NSDateFormatteralloc]
init];
[formatter
setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString* beginStr = [NSStringstringWithFormat:@"%@-01-01
00:00:00",currentYear];
NSDate *beginDate=[formatterdateFromString:beginStr];
//转换生时间戳
beginTime=[beginDate
timeIntervalSince1970];
//时间戳转换成时间
NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:beginTime];
NSString *timeStr=[formatte stringFromDate:currentDate];
日期转换成时间戳:
currentYear=@"2016"
NSDateFormatter *formatter = [[NSDateFormatteralloc]
init];
[formatter
setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString* beginStr = [NSStringstringWithFormat:@"%@-01-01
00:00:00",currentYear];
NSDate *beginDate=[formatterdateFromString:beginStr];
//转换生时间戳
beginTime=[beginDate
timeIntervalSince1970];
//时间戳转换成时间
NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:beginTime];
NSString *timeStr=[formatte stringFromDate:currentDate];
相关文章推荐
- jquery中实现时间戳与日期相互转换
- iOS 第五课 日期时间戳相互转换
- jquery中实现时间戳与日期相互转换
- iOS UTC秒数时间戳和日期的相互转换
- (转)---C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换
- IOS将字符串转换为日期时间格式
- iOS标准时间与时间戳相互转换
- 日期与时间戳之间相互转换
- 日期时间与 time_t 相互转换
- IOS将字符串转换为日期时间格式
- iOS标准时间与时间戳相互转换
- linux下 date命令实现 日期与时间戳互相转换
- javascript时间戳和日期字符串相互转换
- IOS将字符串转换为日期时间格式
- IOS开发笔记-字符串转换为日期时间存在时差问题的处理
- SQL Server本地时间和UTC时间的相互转换实现代码
- postgresql 存储过程函数:时间戳与日期字符串相互转换
- js时间日期和毫秒的相互转换
- SQL Server日期 时间 字符串 相互转换
- JavaScript时间戳与时间日期间相互转换