1

A current time format should be like hh:mm:ss and seconds should run ,the seconds should not stop counting, it should keep on running let me show you what code i am writing.

-(void)timeNotify
{
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate];

_timeLbl.text=dateString;

}

and in view did load i am calling this

-(void)viewDidLoad
{
  [self timeNotify];
}
techno
  • 55
  • 7

3 Answers3

4

You should use NSTimer :

- (void)viewDidLoad
{
  [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(timeNotify)
                                   userInfo:nil
                                    repeats:YES];
}

- (void) timeNotify
{
    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm:ss"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    _timeLbl.text = resultString;
}
Diana Prodan
  • 188
  • 3
  • 7
-1

You should use an NSTimer if you want your time periodically updated

NSTimer* timer =       [NSTimer scheduledTimerWithTimeInterval:1
                                   target:self
                                 selector:@selector(timeNotify)
                                 userInfo:nil
                                  repeats:YES]; 

Remember to set timer to nil when the view disappear because timer is keeping reference to your ViewController and so it will lead to memory leak if you don't destroy it

CZ54
  • 5,488
  • 1
  • 24
  • 39
-2
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];
NSLog(@"User's current time in their preference format:%@",currentTime);
Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24