0

In my iPhone app I want to have a UILabel which counts down to a certain date.

So far I have the user select a date from a UIDatePicker, but I can't figure out how to actually logically countdown each unit of the date. It needs to be live as well, so you can watch the date change rather than having to reload the view and see the difference from now until then.

I want to have a format like so (might change it up later, not sure): dd / mm / yyyy

jscs
  • 63,694
  • 13
  • 151
  • 195
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • Well I have managed to get the date difference in seconds then calculate the days, months and years etc from that. The only trouble is there is daylight saving, and other things like 30 or 31 days in different months which is hardtop account for. I need to get an accurate difference between todays date and the chosen future date. – Josh Kahane Dec 04 '11 at 22:52

1 Answers1

3

You will want an NSTimer to fire every time you wish to update the label and then you need to use NSDate's timeIntervalSinceNow method to get the time interval between your date and now. Then use that to update the label.

For instance:

- (void)startTimer {
    ...
    // Set the date you want to count from
    self.countdownDate = [NSDate date...]; ///< Get this however you need

    // Create a timer that fires every second repeatedly and save it in an ivar
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
    ...
}

- (void)updateLabel {
    NSTimeInterval timeInterval = [self.countdownDate timeIntervalSinceNow]; ///< Assuming this is in the future for now.

    /**
     * Work out the number of days, months, years, hours, minutes, seconds from timeInterval.
     */

    self.countdownLabel.text = [NSString stringWithFormat:@"%@/%@/%@ %@:%@:%@", days, months, years, hours, minutes, seconds];
}

To get the actual number of days, months, years, hours, minutes & seconds you could also use NSCalendar and in particular the components:fromDate:toDate:options: method. Take a look at this question for some more discussion on that: Number of days between two NSDates

Community
  • 1
  • 1
mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • You just opened my eyes all of a sudden. Its one of those days. One thing though, once I have worked out the difference in dates, I can get the seconds. Then lets say you divide that by 60 for minutes then again for hours and then by 24 for days. Then what? I could keep going but each month has a different number of days, how do I get past this? – Josh Kahane Dec 04 '11 at 20:49
  • Don't forget Daylight Saving Time crazyness. They keep changing each year! Absolutely impossible to code all rules inside your app - so how about you just ignore them in the long run and rely on getting it right when time ticks closer to target time? – JOM Dec 04 '11 at 20:54
  • Oh crums of course. Is there no accurate way of retrieving the difference between now and the picked date? I have been looking at the NSCalendar but I'm unsure if it will produce the desired results. – Josh Kahane Dec 04 '11 at 21:02