Developer
Developer

Reputation: 6465

Create a countdown timer in iPhone App

I want to use a countdown timer in my app. I have this date

03-09-2011T20:54:18Z

Now I want to use countdown timer for this date. Can you please suggest if there is any method regarding this. I f I am not clear at any point please let me know. Thanks in advance.

Upvotes: 0

Views: 1935

Answers (1)

MahatmaManic
MahatmaManic

Reputation: 953

Turn that timestamp into an NSDate* (see here)

Then

NSTimeInterval interval = [timestamp timeIntervalSinceNow];

will always give you the number of seconds between now and the date you care about. Then you can do something like:

#define SECONDS_IN_MINUTE 60
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * 60)
#define SECONDS_IN_DAY (SECONDS_IN_HOUR * 24)

NSInteger days, hours, minutes, seconds;
days = interval / SECONDS_IN_DAY;
interval %= SECONDS_IN_DAY;
hours = interval / SECONDS_IN_HOUR;
interval %= SECONDS_IN_HOUR;
minutes = interval / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;

If you want it to update "live" every second then use performSelector:withObject:afterDelay: with a delay of whatever period you want to update the display (or just 0.0 to do it every run loop)

Upvotes: 3

Related Questions