获取当前时间 [英] Getting current time

查看:77
本文介绍了获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前的小时数和秒数.这是我使用的代码

I'm trying to get current hour minutes and seconds. Here is the code I use

NSDate *now = [NSDate date];

NSCalendar *gregorian = [[NSCalendar  alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];

[self.timerLabel setText:[NSString stringWithFormat:@"%@ %@ %@", [dateComponents hour], [dateComponents minute], [dateComponents second]]];

当我在self.timerLabel所在的行上运行应用程序崩溃时.

When I run the app crash on the line where self.timerLabel is.

这是怎么了?

推荐答案

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; 

NSInteger hour= [components hour];   
NSInteger minute = [components minute];
NSInteger second = [components second]; 

这篇关于获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆