James Dunay
James Dunay

Reputation: 2724

Iphone SDK: How to Countdown from current time to a specific day in the future

I have was wondering how it would be possible to show the specific time remaining for a date in the future.

This is what i have so far. I can get the current time, and display that, and i used the minutes to midnight tutorial to figure out how to find out what time it will be midnight. But i am suck as to finding out how i would pick a day in the future and find out how much time is left there.

code:

NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];

    NSInteger hour = 23 - [dateComponents hour];
    NSInteger minute = 59 - [dateComponents minute];
    NSInteger second = 59 - [dateComponents second];
    [gregorian release];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];

Any possibility someone could take a look at this and change it up a bit? It would be easiest for me to follow if i could understand it from using some of this code, but if thats not possible or correct I would rather know the right way to do it.

Thanks.

Upvotes: 1

Views: 4044

Answers (1)

Alex Gosselin
Alex Gosselin

Reputation: 2930

Try one of these, it will be number of second between the two dates in double:

 NSTimeInterval timeBetweenThenAndNow = [futureDate timeIntervalSinceNow];  
 NSTimeInterval timeBetweenThenAndMidnight = [futureDate timeIntervalSinceDate: myMidnightDate]; 

Upvotes: 2

Related Questions