Jenifer
Jenifer

Reputation: 1

how to get iphone time

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

Upvotes: 0

Views: 98

Answers (2)

Joshua
Joshua

Reputation: 15520

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];

Upvotes: 1

Gauloises
Gauloises

Reputation: 2046

NSDate *today = [NSDate date];

Gives you the current time and date.

Upvotes: 5

Related Questions