开发者

getting current hour, minutes and seconds in iPhone

开发者 https://www.devze.com 2022-12-21 20:16 出处:网络
How can I get the current hour, minutes, and seconds from this? NSDate *now =开发者_StackOverflow [NSDate date];

How can I get the current hour, minutes, and seconds from this?

NSDate *now =开发者_StackOverflow [NSDate date];

Thanks! I want to have 3 int variables with the values stored in them, not a string which displays the values.


You need to use NSDateComponents, which is a bit tricky (not to mention verbose!) since you need to understand a bit about the way Cocoa handles calendars. There's a great intro in the docs; here's a modified excerpt:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components =
                    [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];

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

精彩评论

暂无评论...
验证码 换一张
取 消