Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting current hour, minutes and seconds in iPhone

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

NSDate *now = [NSDate date];

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

like image 777
Manuel Araoz Avatar asked Mar 02 '10 05:03

Manuel Araoz


People also ask

Can iPhone display time with seconds?

The Clock app icon on the home screen of iOS devices shows seconds.


1 Answers

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];
like image 73
Jesse Beder Avatar answered Nov 03 '22 03:11

Jesse Beder