user21293
user21293

Reputation: 6469

Retrieving current local time on iPhone?

I'm looking to get the current hour and minute on a user's iPhone for display in an app that doesn't show the status bar. Is there a simple way to do this?

Upvotes: 32

Views: 68161

Answers (7)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

CFAbsoluteTimeGetCurrent()

Absolute time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT. A positive value represents a date after the reference date, a negative value represents a date before it. For example, the absolute time -32940326 is equivalent to December 16th, 1999 at 17:54:34. Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.

Upvotes: 0

Jim75
Jim75

Reputation: 767

A shorter approach

NSDate * now = [NSDate date];
timeLabel.text = [NSDateFormatter localizedStringFromDate:now
                                                dateStyle:NSDateFormatterNoStyle
                                                timeStyle:NSDateFormatterShortStyle];

Upvotes: 0

Nitin Malguri
Nitin Malguri

Reputation: 201

-(void)currentTime
{    
    //Get current time
    NSDate* now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponents = [gregorian components:(NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:now];
    NSInteger hour = [dateComponents hour];
    NSString *am_OR_pm=@"AM";

    if (hour>12)
    {
        hour=hour%12;

        am_OR_pm = @"PM";
    }

    NSInteger minute = [dateComponents minute];
    NSInteger second = [dateComponents second];
    [gregorian release];

    NSLog(@"Current Time  %@",[NSString stringWithFormat:@"%02ld:%02ld:%02ld %@", (long)hour, (long)minute, (long)second,am_OR_pm]);
}

Upvotes: 13

Chris Ballance
Chris Ballance

Reputation: 34347

See this similar question for an answer. You will have to change it to your date format.

[[NSDate date] timeIntervalSince1970];

Upvotes: 10

Vinay W
Vinay W

Reputation: 10190

if you are looking to calculate time intervals, you are better off using CACurrentMediaTime

double currentTime = CACurrentMediaTime();

Upvotes: 6

ZYiOS
ZYiOS

Reputation: 5280

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]]
[dateFormatter release]; dateFormatter = nil;

I think you should try this. The timeZone is important.

Upvotes: 12

mixolution
mixolution

Reputation: 1121

// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(@"User's current time in their preference format:%@",currentTime);

Upvotes: 112

Related Questions