tags:

views:

40

answers:

1

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.

+1  A: 

Try one of these:

 NSTimeInterval *timeBetweenThenAndNow = [futureDate timeIntervalSinceNow];
 NSTimeInterval *timeBetweenThenAndMidnight = [futureDate timeIntervalSinceDate: myMidnightDate];
Alex Gosselin
Sorry to misunderstand but could you be more specific as to where i should try these?
James Dunay
You should use these to obtain the length of time between one date and another, or between now and another date. NSDateFormatter will help with printing the result, which is a NSTimeInterval - the number of seconds between. Take a look at the class references, and tell me a bit more about what you're trying to do if you are still stuck.
Alex Gosselin
ok, hmm still a little stuck, but I think I know what needs to happen I just missing it when I try to put it together. I need to define two separate dates. One will be the current time which I can get by using this? -- NSDate* now = [NSDate date]; -- then I need to set a date in the future say September 15, 2010 (which might look something like this? -- NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"]; NSString *future = [[NSString alloc] initWithString:@"09/15/2010 12:00:00 am"]; --) I guess an example would help.
James Dunay
You're almost there with the dateformatter, take a look at the class reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html the idea is if you want the difference between now and your future date, don't bother making the *now date, just use [futureDate timeIntervalSinceNow];
Alex Gosselin