Different display by NSDate and NSDateFormatter

Discussion in 'iOS Programming' started by namanhams, Feb 22, 2011.

  1. namanhams, Feb 22, 2011
    Last edited: Feb 22, 2011

    namanhams macrumors regular

    Jun 3, 2009
    Here is my code :
    NSString *s = @"Wednesday, 19 January 2011";
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"EEEE, dd LLLL yyyy"];
    NSDate *date = [df dateFromString:s];
    NSLog(@"%@",[df stringFromDate:date]); 
    Here's the output :

    2011-01-18 16:00:00 +0000
    Wednesday, 19 January 2011

    So the 2nd output line is as expected, while the 1st output line is totally strange.
    How come it's like that ?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    NSDate has no locale or time zone associated with it. When you log it like that it logs as if it were in GMT. This was a change, I think in 4.0. Before that it would print the date using your own locale and time zone. You should never use something like printing a date using %@ to show a date to a user. Use the date formatter, and it will give you what you want (once you figure it out).
  3. namanhams thread starter macrumors regular

    Jun 3, 2009

Share This Page