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
2010-08-27 13:08:29
+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
2010-08-27 13:47:42
Don't forget to release your calendar when you're done with it.
Jonathan Grynspan
2010-08-27 14:39:39
Thanks for reminding me!
Joshua
2010-08-27 15:15:27