tags:

views:

42

answers:

2

can you help out in finding the iphone time through iphone application

+5  A: 
NSDate *today = [NSDate date];

Gives you the current time and date.

Gauloises
+1  A: 
NSDate *today = [NSDate date];  

NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:today];

[calendar release];    

NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];

This will actually get just the time from the date, not the whole date. It gets the time in separate integers, hour, min, sec. To put them all back together in a string you can do the following:

NSString *time = [NSString stringWithFormat:@"%i:%i:%i", hour, min, sec];
Joshua
Don't forget to release your calendar when you're done with it.
Jonathan Grynspan
Thanks for reminding me!
Joshua