I am trying to get a countdown timer to a specific date. I use:
-(void) viewWillAppear:(BOOL)animated {
NSString *str =@"12/27/2013";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [formatter dateFromString:str];
NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow];
NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400;
startTime = [[NSDate date] retain];
secondsLeft = numberOfDays;
NSLog(@"number%f", numberOfSecondsUntilSelectedDate);
[self countdownTimer];
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
secondsLeft -- ;
days = secondsLeft / 86400;
NSLog(@"%d", days);
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
myCounterLabel.text = [NSString stringWithFormat:@"%02d Days %02d Hours %02d Minutes %02d Seconds", days, hours, minutes, seconds];
}
else{
secondsLeft = 16925;
}
}
-(void)countdownTimer{
secondsLeft = hours = minutes = seconds = 0;
if([timer isValid])
{
[timer release];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[pool release];
}
However, the day keeps coming out as 0. What am I doing wrong?