32

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?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user21293
  • 6,439
  • 11
  • 44
  • 57

7 Answers7

112
// 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);
mixolution
  • 1,121
  • 1
  • 7
  • 2
  • +1 It should be noted that you can change to show days and months and what not by editing the piece of code saying "setTimeStyle" to something like "setDateStyle" – Albert Renshaw Jan 03 '13 at 06:12
  • 4
    If your Xcode project is using ARC (which it should be by default in 2014) then you don't need to call release on dateFormatter. – John Pavley Aug 16 '14 at 16:28
13
-(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]);
}
Muhammad Noman
  • 1,566
  • 1
  • 15
  • 21
Nitin Malguri
  • 201
  • 3
  • 7
12
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.

ZYiOS
  • 5,204
  • 3
  • 39
  • 45
10

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

[[NSDate date] timeIntervalSince1970];
Community
  • 1
  • 1
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
6

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

double currentTime = CACurrentMediaTime();
Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

A shorter approach

NSDate * now = [NSDate date];
timeLabel.text = [NSDateFormatter localizedStringFromDate:now
                                                dateStyle:NSDateFormatterNoStyle
                                                timeStyle:NSDateFormatterShortStyle];
Jim75
  • 737
  • 8
  • 13
0

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.

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184