IOS 时间和时间戳之间转化示例
更新时间:2017年01月18日 10:54:35 作者:小眼奇遇记
我们经常从服务器后台拿到时间戳的时间,以下代码可以实现将时间戳转为可读的时间格式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
以毫秒为整数值的时间戳转换
时间戳转化为时间NSDate
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString { // 格式化时间 NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"]; // 毫秒值转化为秒 NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0]; NSString* dateString = [formatter stringFromDate:date]; return dateString; }
时间转化为时间戳
// 当前时间 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒 NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
通过比较时间与当前时间返回年月日的方法
- (void)getBabyDetailAge:(NSString *)date { // 获得日期对象 NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init]; formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *createDate = [formatter_ dateFromString:date]; NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian]; NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear; NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ]; NSInteger years = [components year]; NSInteger months = [components month ]; NSInteger days = [components day ]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
浅谈iOS中几个常用协议 NSCopying/NSMutableCopying
下面小编就为大家分享一篇浅谈iOS中几个常用协议 NSCopying/NSMutableCopying,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2017-12-12iOS自定义UIBarButtonItem的target和action示例代码
这篇文章主要给大家介绍了关于iOS自定义UIBarButtonItem的target和action的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-02-02详解iOS App中UISwitch开关组件的基本创建及使用方法
UISwitch组件就是我们平时在iOS设置菜单中开到的那种左右滑动的开关按钮,当然我们在开发时可以进行更多的自定义,这里我们就来详解iOS App中UISwitch开关组件的基本创建及使用方法2016-05-05iOS中利用UIBezierPath + CAAnimation实现心跳动画效果
这篇文章主要给大家介绍了关于iOS中利用UIBezierPath + CAAnimation实现心跳动画效果的相关资料,文中通过示例代码介绍的非常详细,对大家的日常开发具有一定的参考学习,需要的朋友们下面随着小编来一起学习学习吧。2017-10-10
最新评论