码迷,mamicode.com
首页 > 其他好文 > 详细

oc之获取系统当前时间的方法

时间:2016-01-07 18:07:50      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

        
NSDate *date = [NSDate date];//这个是NSDate类型的日期,所要获取的年月日都放在这里; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth| NSCalendarUnitDay;//这句是说你要获取日期的元素有哪些。获取年就要写NSYearCalendarUnit,获取小时就要写NSHourCalendarUnit,中间用|隔开;
NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面; //然后就可以从d中获取具体的年月日了;
NSInteger year = [d year];
NSInteger month
= [d month];
NSInteger day
= [d day];
NSLog(
@"%ld",year);
NSLog(
@"%ld",month);
NSLog(
@"%ld",day);

 

oc之获取系统当前时间的方法

标签:

原文地址:http://www.cnblogs.com/jiangyicheng/p/5110571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!