iOS NSDate时间换算
2016-06-14 17:00
399 查看
iOS开发过程中,经常会用到时间,比如今天相对于过去某一天相差多少天,多少小时,多少分钟等等,还要换算,啊啊啊啊啊,什么加减乘除,什么12,什么60,什么24,统统滚蛋吧,今天为了“时间”,我也是拼了,下面我把经常用的时间一一列举出来了,希望能够对读者有所帮助;
在一个ViewController里面开始吧,gogogogoogogogo;
输出结果:
2016-06-14 16:46:12.651 Timer[2811:639641] nowTime = 2016-06-14 16:46:12
2016-06-14 16:46:12.654 Timer[2811:639641]相差世纪个数 = 0
2016-06-14 16:46:12.654 Timer[2811:639641]相差年个数 = 1
2016-06-14 16:46:12.654 Timer[2811:639641]相差月个数 = 23
2016-06-14 16:46:12.654 Timer[2811:639641]相差小时个数 = 17541
2016-06-14 16:46:12.654 Timer[2811:639641]相差分钟个数 = 1052481
2016-06-14 16:46:12.654 Timer[2811:639641]相差秒个数 = 63148872
2016-06-14 16:46:12.654 Timer[2811:639641]距离1970年有多少秒
= 1465893972.649262
2016-06-14 16:46:12.654 Timer[2811:639641]距离现在有多少秒 = 63148872.654635
如果转载请注明转于:AirZilong的博客
在一个ViewController里面开始吧,gogogogoogogogo;
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1、获取当前时间 NSDate *now = [NSDate date]; NSDateFormatter *nowFormate = [[NSDateFormatter alloc] init]; nowFormate.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *nowTime = [nowFormate stringFromDate:now]; NSLog(@"nowTime = %@",nowTime); // 2、拿现在的时间和过去时间或者将来时间对比,计算出相差多少天,多少年,多少秒等等; NSDate *beforTime = [nowFormate dateFromString:@"2014-06-14 19:25:00"]; NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; //世纪 NSInteger era = kCFCalendarUnitEra; //年 NSInteger year = kCFCalendarUnitYear; //月 NSInteger month = kCFCalendarUnitMonth; //小时 NSInteger hour = kCFCalendarUnitHour; //分钟 NSInteger minute = kCFCalendarUnitMinute; //秒 NSInteger second = kCFCalendarUnitSecond; NSDateComponents *compsEra = [calender components:era fromDate:beforTime toDate:now options:0]; NSDateComponents *compsYear = [calender components:year fromDate:beforTime toDate:now options:0]; NSDateComponents *compsMonth = [calender components:month fromDate:beforTime toDate:now options:0]; NSDateComponents *compsHour = [calender components:hour fromDate:beforTime toDate:now options:0]; NSDateComponents *compsMinute = [calender components:minute fromDate:beforTime toDate:now options:0]; NSDateComponents *compsSecond = [calender components:second fromDate:beforTime toDate:now options:0]; NSLog(@"相差世纪个数 = %ld",[compsEra era]); NSLog(@"相差年个数 = %ld",[compsYear year]); NSLog(@"相差月个数 = %ld",[compsMonth month]); NSLog(@"相差小时个数 = %ld",[compsHour hour]); NSLog(@"相差分钟个数 = %ld",[compsMinute minute]); NSLog(@"相差秒个数 = %ld",[compsSecond second]); // 3、获取时间戳(相对于1970年) CGFloat timestamp = now.timeIntervalSince1970; NSLog(@"距离1970年有多少秒 = %f",timestamp); // 4、计算距离现在有多少秒 CGFloat sinceNow = beforTime.timeIntervalSinceNow; NSLog(@"距离现在有多少秒 = %f",fabs(sinceNow)); } @end
输出结果:
2016-06-14 16:46:12.651 Timer[2811:639641] nowTime = 2016-06-14 16:46:12
2016-06-14 16:46:12.654 Timer[2811:639641]相差世纪个数 = 0
2016-06-14 16:46:12.654 Timer[2811:639641]相差年个数 = 1
2016-06-14 16:46:12.654 Timer[2811:639641]相差月个数 = 23
2016-06-14 16:46:12.654 Timer[2811:639641]相差小时个数 = 17541
2016-06-14 16:46:12.654 Timer[2811:639641]相差分钟个数 = 1052481
2016-06-14 16:46:12.654 Timer[2811:639641]相差秒个数 = 63148872
2016-06-14 16:46:12.654 Timer[2811:639641]距离1970年有多少秒
= 1465893972.649262
2016-06-14 16:46:12.654 Timer[2811:639641]距离现在有多少秒 = 63148872.654635
如果转载请注明转于:AirZilong的博客
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- iOS开发之路--微博“更多”页面
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法