0

I have to run a countdown timer for a specific interval but how to feed self.startDate in this function

- (void)updateCounter:(NSTimer *)theTimer {

    NSDate *now = [NSDate date];
    // has the target time passed?
    if ([self.startDate earlierDate:now] == self.startDate) {
        [theTimer invalidate];
    } else {
        NSUInteger flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:now toDate:self.startDate options:0];

        NSLog(@"there are %d hours, %d minutes and %d seconds remaining", [components hour], [components minute], [components second]);
    }
}
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • you want to setStartDate from which time/string? – Anoop Vaidya Dec 13 '13 at 09:18
  • -1 This is very basic NSDateFormatter stuff. If you had tried *at all* to look it up you would have found your answer. – Hot Licks Dec 13 '13 at 12:34
  • possible duplicate of [Converting NSString to NSDate (and back again)](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again) – Ben Flynn Dec 14 '13 at 03:06

1 Answers1

1

Convert NSString to NSDate using:

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"HH:mm:ss"];
  NSDate *date = [dateFormatter dateFromString:yourString];
Nikos M.
  • 13,685
  • 4
  • 47
  • 61