0

So I'm trying to get the time of a specific time zone and displaying it as a label on my storyboard. The time displayed is correct except it doesn't update as time passes by. What am I missing? Here's the code:

- (void)viewDidLoad {
# pragma mark TimeZones

    NSDate *now = [NSDate date];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"hh:mm"];
    [formatter2 setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];
    NSLog(@"%@",[formatter2 stringFromDate:now]); //--> 11:54 PM
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];
    NSLog(@"%@",[formatter2 stringFromDate:now]);
    NSString *strSelectedDate= [formatter stringFromDate:now];
    self.zone.text = strSelectedDate;
}
Infineon Labs
  • 91
  • 1
  • 11
  • 2
    Why should it update as time passes? viewDidLoad is run once when the view first loads. You're not doing anything to update the label. – rdelmar Oct 23 '14 at 16:44
  • I tried using the (void)updateDate function but that didn't work either. Would you mind telling me what I'm missing? I'm new to all of this. @rdelmar – Infineon Labs Oct 23 '14 at 16:49
  • You should create a repeating timer, and put the code you have in your question into the timer's action method. That way, each time the method is called, [NSDate date] will give you the current date and time. – rdelmar Oct 23 '14 at 16:51

0 Answers0