0

I'm creating a simple timer but am failing to get the resulting date that I'm after.

My problem is that I'm getting -34481.274523 from my NSTimeInterval (nslog at the end) whereas I'm wanting the number of seconds since the current date (currentDate). stopDate is always a date before currentDate.

The code snippet below is called from an nstimer every second. Rather than simply increment an int I'm using a date as it's easier to perform other date related calculations on and format for display.

Here is my code:

NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:stopDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];

NSString *timeString=[dateFormatter stringFromDate:timerDate];

NSLog(@"nstimeinterval: %f", timeInterval);

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: ( NSHourCalendarUnit | NSYearCalendarUnit) fromDate:timerDate];
NSLog(@"year: %li", (long)components.year);
if (components.year == 2000) {
    timeString = @"00:00";

    return;
}

NSLog(@"timer: %@", timeString);

timerTicksForCounter = timeString;

NSLog(@"hour: %li", (long)components.hour);

//Used to update uilabel
[self updateTimerLabel];


if (components.year < 2000) {
    return;
}

if (components.hour == 0) {

..... I'm doing a bunch of stuff here
user1542125
  • 593
  • 6
  • 16

0 Answers0