NSDateFormatter dateFromString returing incorrect result [Resolved]

Discussion in 'Mac Programming' started by McBgnr, May 6, 2009.

  1. McBgnr macrumors regular

    Joined:
    Apr 13, 2009
    #1
    Hello,

    I am trying to get the date from a dateString using the following code snippet:

    Code:
    NSDate *dateToday = [NSDate date];
    	NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    	[dateFormat setDateFormat:@"MMM d, YYYY"];
    	NSString *sDate = [dateFormat stringFromDate:dateToday];
    	NSLog(sDate);
    	
    	NSDate *newDate = [dateFormat dateFromString:sDate];
    	
    	NSString *newDateString = [dateFormat stringFromDate:newDate];
    	NSLog(newDateString);
    	
    
    But the value that is getting logged for newDateString is Dec 22, 2008 instead of today's date. Am I missing something in my code? Suggestions are welcome.
     
  2. kanenas macrumors newbie

    Joined:
    Jun 20, 2008
    #2
    'Y' is for non-Gregorian ISO week date

    The 'Y' format specifier is for the ISO week date, a governmental and fiscal calendar that has 364 or 371 days in a year so as to be evenly divisible into weeks. Use 'y' for the Gregorian year. The Unicode date format specs (tr35-4 for OS 10.4 and tr35-6 for 10.5) give the meaning of the 'Y' and 'y' format characters.
     
  3. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009

Share This Page