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?
-
are you creating iphone web app or iphone app – Ashutosh Singh-MVP SharePoint Feb 19 '09 at 17:22
-
6Ashutosh Singh: You can see by the tags that he's talking about a native app. – Peter Hosey Feb 19 '09 at 17:31
-
See the documentation for NSDate, NSDateComponents, and NSDateFormatter. – Kristopher Johnson Feb 19 '09 at 17:28
7 Answers
// 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);

- 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
-
4If 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
-(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]);
}

- 1,566
- 1
- 15
- 21

- 201
- 3
- 7
-
had issue with 12 PM case so fixed it with if (hour>=12) { if (hour!=12) hour=hour%12; am_OR_pm = @"PM"; } – Francis F Apr 30 '14 at 07:44
-
2
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.

- 5,204
- 3
- 39
- 45
See this similar question for an answer. You will have to change it to your date format.
[[NSDate date] timeIntervalSince1970];

- 1
- 1

- 33,810
- 26
- 104
- 151
if you are looking to calculate time intervals, you are better off using CACurrentMediaTime
double currentTime = CACurrentMediaTime();

- 9,912
- 8
- 41
- 47
A shorter approach
NSDate * now = [NSDate date];
timeLabel.text = [NSDateFormatter localizedStringFromDate:now
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterShortStyle];

- 737
- 8
- 13
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.

- 1
- 1

- 39,458
- 17
- 135
- 184