NSDateFormatter returning wrong date

Discussion in 'iOS Programming' started by ross.32, Dec 26, 2009.

  1. ross.32 macrumors 6502

    Joined:
    May 27, 2007
    #1
    Using this code here, NSDateFormatter is returning the year as 2010. Anyone else experiencing this?

    Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"YYYYMMddHHmm"];
    NSString *dateString = [[NSString alloc] initWithFormat:@"%@", [dateFormatter stringFromDate:[NSDate date]]];
    //NSLog(@"The dateString = %@", dateString);
    [dateFormatter release];
     
  2. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    That's correct. The date format specifier "Y" gives you the year number to be used in combination with week numbers. In UTC we're currently on week 1 2010, that's why the "YYYY" format returns "2010". If you want the ordinary year you should use "yyyy".
     

Share This Page