0

I'm making a clock app, and I want to display the current time, with the components (hours minutes, seconds) spaced out. I think that the only way to do that is to separate the time into three strings. How do I do that?

- (void)updateTime {

    [updateTimer invalidate];
    updateTimer = nil;

    currentTime = [NSDate date];
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
    lblTime.text = [timeFormatter stringFromDate:currentTime];

    updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
user2397282
  • 3,798
  • 15
  • 48
  • 94

3 Answers3

4

A couple things here:

1 ) if you want a constantly running timer, don't invalidate it each time "updateTime" is called (and only call scheduledTimerWithTimeInterval: once... instead of at the end of each time "updateTime" is called).

2 ) it looks like you have a single label for displaying your time. You should make your timeFormatter an ivar (instance variable, so it's created and set only once) and then you can set the format via:

timeFormatter.dateFormat = @"HH:mm:ss";

And you should be all set.

Your new updateTime method could look like this:

- (void)updateTime {    
    currentTime = [NSDate date];
    lblTime.text = [timeFormatter stringFromDate:currentTime];    
}

3 ) If you want three different labels, you need to declare IBOutlets for the three labels and have three different date formatters. For example:

@interface YourViewController : UIViewController ()
{
    IBOutlet UILabel * hourLabel; // you need to connect these in your XIB/storyboard
    IBOutlet UILabel * minuteLabel;
    IBOutlet UILabel * secondLabel;

    NSDateFormatter * hourFormatter;
    NSDateFormatter * minuteFormatter;
    NSDateFormatter * secondFormatter;
}

Then, in your "viewDidLoad:" method, set up your formatters:

hourFormatter = [[NSDateFormatter alloc] init;
hourFormatter.dateFormatter = @"HH";
minuteFormatter = [[NSDateFormatter alloc] init;
minuteFormatter.dateFormatter = @"mm";
secondFormatter = [[NSDateFormatter alloc] init;
secondFormatter.dateFormatter = @"ss";

And finally, in your "updateTime" method:

- (void)updateTime {    
    currentTime = [NSDate date];
    if(hourFormatter)
    {
        if(hourLabel)
        {
            hourLabel.text = [hourFormatter stringFromDate: currentTime];
        } else {
            NSLog( @"you need to connect your hourLabel outlet in your storyboard or XIB" );
        }
    } else {
        NSLog( @"you need to allocate and init and set hourFormatter");
    }
    minuteLabel.text = [minuteFormatter stringFromDate: currentTime];
    secondLabel.text = [secondFormatter stringFromDate: currentTime];
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Sorry, I'm new to Objective-C, will this create three separate strings? I want to be able to have three labels for hours, minutes, seconds, so that I can place them in different locations on the view. – user2397282 May 18 '13 at 16:59
  • 1
    @user2397282 http://stackoverflow.com/questions/3694867/nsdate-get-year-month-day – Lorenzo B May 18 '13 at 17:02
  • Sorry again, but I've done exactly as you've said, but it just doesn't show up – user2397282 May 18 '13 at 17:21
  • With the 'updateTime' method, is that all the code I need; do I need to keep anything I started with? – user2397282 May 18 '13 at 17:32
  • In order for this timer to run, you need to "keep" the three formatters and the three label outlets and a timer. What else do you mean by "need to keep anything I started with?". – Michael Dautermann May 18 '13 at 17:34
  • Woops, I deleted the current time variable and that's why it wasn't working! Thank you ever so much, you've been fantastic help! – user2397282 May 18 '13 at 17:38
  • Using `NSDateComponents` would be a much better choice than creating three `NSDateFormatter` objects and formatting an `NSDate` three times. – rmaddy May 18 '13 at 17:45
0

If you just need a single string, you can change

[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];

to

[timeFormatter setDateFormat:@"hh:mm:ss"];
Craig
  • 691
  • 4
  • 20
0

You can use to separate hh, mm, ss by using

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds = [components second];
Noor
  • 2,071
  • 19
  • 28