Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hy, try to use this:</p> <p>Put this in <strong><em>-(void)viewDidLoad</em></strong> method</p> <pre><code>NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES]; </code></pre> <p>then create the <strong><em>-(void)countDownTimer</em></strong> method</p> <pre><code>- (void)countDownTimer { // my method which returns the differences between two dates in my case double diff = [self getDateDifference]; double days = trunc(diff / (60 * 60 * 24)); double seconds = fmod(diff, 60.0); double minutes = fmod(trunc(diff / 60.0), 60.0); double hours = fmodf(trunc(diff / 3600.0), 24); if(diff &gt; 0) { NSString *countDownString = [NSString stringWithFormat:@"%02.0f day(s)\n%02.0f:%02.0f:%02.0f", days, hours, minutes, seconds]; // IBOutlet label, added in .h self.labelCountDown.text= countDownString; } else { // stoping the timer [timer invalidate]; timer = nil; // do something after countdown ... } } </code></pre> <p>and you can add a <strong><em>- (double)getDateDifference</em></strong> method which returns the difference between two dates in my case</p> <pre><code>- (double)getDateDifference { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *dateFromString = [[NSDate alloc] init]; NSDate *now = [NSDate date]; NSString *myDateString = @"2013-10-10 10:10:10"; // my initial date with time // if you want to use only time, than delete the // date in myDateString and setDateFormat:@"HH:mm:ss" // this line is not required, I used it, because I need GMT+2 [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+2]]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // get the date dateFromString = [dateFormatter dateFromString:myDateString]; // this line is also not required, I used it because I need GMT+2 // so I added two hours in seconds to 'now' now = [now dateByAddingTimeInterval:60*60*2]; // getting the difference double diff = [dateFromString timeIntervalSinceDate:now]; NSLog(@"dateString: %@", dateString); NSLog(@"now: %@", now); NSLog(@"targetDate: %@", dateFromString); NSLog(@"diff: %f", diff); return diff; } </code></pre> <p>the output is similar to this</p> <pre><code>dateString: 2013-10-10 00:20:00 now: 2013-10-10 00:20:00 +0000 target: 2013-10-10 00:20:28 +0000 diff: 28.382786 </code></pre> <p>I hope it was helpfull</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload