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

获取当前的日期和时间-数码

时间:2015-07-02 13:34:14      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

IOS---如何获取当前的日期和时间,并显示

 
创建两个UILable: Lable1,Lable2.创建好IBOutlate并连线, 然后用如下方法:

-(void)updateLabel {

NSDate* now = [NSDate date];

 NSCalendar *cal = [NSCalendar currentCalendar];


unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;

NSDateComponents *dd = [cal components:unitFlags fromDate:now];

int y = [dd year];

int m = [dd month];

int d = [dd day];

    

int hour = [dd hour];

int min = [dd minute];

int sec = [dd second];

 

Lable1.text = [NSString stringWithFormat:@"2d%:2d%:2d%",y,m,d];

Lable2.text =[NSStrinstringWithFormat:@"2d%:2d%:2d%",hour,min,sec];

}

 

- (void)onTimer {

[viewController updateLabel];

}

 

在AppDeletate.m文件中增加如下方法:

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {

timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

    [window addSubview:viewController.view];

[window makeKeyAndVisible];

}

 

技术分享

获取当前的日期和时间-数码

标签:

原文地址:http://www.cnblogs.com/niexiaobo/p/4615567.html

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