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
You will want an
NSTimerto fire every time you wish to update the label and then you need to useNSDate‘stimeIntervalSinceNowmethod to get the time interval between your date and now. Then use that to update the label.For instance:
To get the actual number of days, months, years, hours, minutes & seconds you could also use
NSCalendarand in particular thecomponents:fromDate:toDate:options:method. Take a look at this question for some more discussion on that:Number of days between two NSDates