打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS NSDate时间换算


  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.       
  12.       
  13.       
  14.     // 1、获取当前时间  
  15.     NSDate *now = [NSDate date];  
  16.     NSDateFormatter *nowFormate = [[NSDateFormatter alloc] init];  
  17.     nowFormate.dateFormat = @"yyyy-MM-dd HH:mm:ss";  
  18.     NSString *nowTime = [nowFormate stringFromDate:now];  
  19.     NSLog(@"nowTime = %@",nowTime);  
  20.       
  21.     // 2、拿现在的时间和过去时间或者将来时间对比,计算出相差多少天,多少年,多少秒等等;  
  22.     NSDate *beforTime = [nowFormate dateFromString:@"2014-06-14 19:25:00"];  
  23.     NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];  
  24.       
  25.     //世纪  
  26.     NSInteger era  = kCFCalendarUnitEra;  
  27.       
  28.     //年  
  29.     NSInteger year = kCFCalendarUnitYear;  
  30.       
  31.     //月  
  32.     NSInteger month = kCFCalendarUnitMonth;  
  33.       
  34.     //小时  
  35.     NSInteger hour = kCFCalendarUnitHour;  
  36.       
  37.     //分钟  
  38.     NSInteger minute = kCFCalendarUnitMinute;  
  39.       
  40.     //秒  
  41.     NSInteger second = kCFCalendarUnitSecond;  
  42.       
  43.     NSDateComponents *compsEra = [calender components:era fromDate:beforTime toDate:now options:0];  
  44.     NSDateComponents *compsYear = [calender components:year fromDate:beforTime toDate:now options:0];  
  45.     NSDateComponents *compsMonth = [calender components:month fromDate:beforTime toDate:now options:0];  
  46.     NSDateComponents *compsHour = [calender components:hour fromDate:beforTime toDate:now options:0];  
  47.     NSDateComponents *compsMinute = [calender components:minute fromDate:beforTime toDate:now options:0];  
  48.     NSDateComponents *compsSecond = [calender components:second fromDate:beforTime toDate:now options:0];  
  49.       
  50.     NSLog(@"相差世纪个数 = %ld",[compsEra era]);  
  51.     NSLog(@"相差年个数 = %ld",[compsYear year]);  
  52.     NSLog(@"相差月个数 = %ld",[compsMonth month]);  
  53.     NSLog(@"相差小时个数 = %ld",[compsHour hour]);  
  54.     NSLog(@"相差分钟个数 = %ld",[compsMinute minute]);  
  55.     NSLog(@"相差秒个数 = %ld",[compsSecond second]);  
  56.       
  57.     // 3、获取时间戳(相对于1970年)  
  58.     CGFloat timestamp = now.timeIntervalSince1970;  
  59.     NSLog(@"距离1970年有多少秒 = %f",timestamp);  
  60.       
  61.     // 4、计算距离现在有多少秒  
  62.     CGFloat sinceNow = beforTime.timeIntervalSinceNow;  
  63.     NSLog(@"距离现在有多少秒 = %f",fabs(sinceNow));  
  64.       
  65.       
  66. }  
  67.   
  68. @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



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
NSDate NSCalendar(日历立法) NSDateComponents(日期组件)的一些用法
时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
ios中的日期操作的技巧
ios时间那点事
关于NSRunLoop和NSTimer的深入理解 | XCoder Studio
这是一篇写给新手的NSDate教程(Swift版)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服