Search code examples
objective-cformatnsdatecountdown

NSDate Outputs Wrong Date


I am creating a countdown timer to a specific date using NSDate and NSDateFormatter. The only problem is that when I run the code with a different format the date messes up. Here is the original code that works great.

- (void)updateCountdown {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *startingDate = [dateFormatter dateFromString:@"2014-10-08"];
    NSDate *endingDate = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];

    NSInteger days     = [dateComponents day];
    NSInteger months   = [dateComponents month];
    NSInteger years    = [dateComponents year];
    NSInteger hours    = [dateComponents hour];
    NSInteger minutes  = [dateComponents minute];
    NSInteger seconds  = [dateComponents second];
    NSString *countdownText = [NSString stringWithFormat:@"%ld Years %ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)years, (long)hours, (long)minutes, (long)seconds];

    _countdown.text = countdownText;

    [self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}

But say I want to remove the years so I change the code like this:

- (void)updateCountdown {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *startingDate = [dateFormatter dateFromString:@"2014-10-08"];
    NSDate *endingDate = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];

    NSInteger days     = [dateComponents day];
    NSInteger months   = [dateComponents month];
    NSInteger hours    = [dateComponents hour];
    NSInteger minutes  = [dateComponents minute];
    NSInteger seconds  = [dateComponents second];
    NSString *countdownText = [NSString stringWithFormat:@"%ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)hours, (long)minutes, (long)seconds];

    _countdown.text = countdownText;

    [self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}

Now all of a sudden the dat is messed up by a whole month. How may I fix this?

Also why is the output 0 years -1months 0 days -2 Hours.. Like why is there (-) in there? Thanks!


Solution

  • you are putting your arguments in the wrong order first you have

    [NSString stringWithFormat:@"%ld Years %ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)years, (long)hours, (long)minutes, (long)seconds];
    

    you are entering

    • days into a place holder years
    • and years into days

    then you have

    [NSString stringWithFormat:@"%ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)hours, (long)minutes, (long)seconds];
    

    you are entering

    • days into a place holder months
    • months into place holding for days only the last 3 appear to be as you intend