Weird behaviour of NSDateComponents/NSDate

Discussion in 'iOS Programming' started by RossOliver, Mar 17, 2012.

  1. RossOliver macrumors regular

    Nov 6, 2006

    I'm getting very odd results when I NSLog an NSDate created with NSDateComponents:

    NSDate *test;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:26];
    [comps setMonth:3];
    [comps setYear:2012];
    test = [[NSCalendar currentCalendar] dateFromComponents:comps];
    NSLog(@"DAY: %i, MONTH: %i, YEAR: %i, DATE: %@", [test day], [test month], [test year], test);
    // OUTPUTS: DAY: 26, MONTH: 3, YEAR: 2012, DATE: 2012-03-25 23:00:00 +0000
    When you NSLog the NSDate this code produces, it outputs 2012-03-25, not 2012-03-26. However, if I set the components to a date prior to 2012-03-26, such as 2012-03-25, it works perfectly - outputting 2012-03-25 00:00:00. Any date after and including 26th March 2012 logs the date as the previous day with the hour portion set to 23:00:00. I can't for the life of me understand why?

    Any help greatly appreciated!


  2. quellish macrumors newbie

    Apr 19, 2010
    Sounds like Daylight savings in the UK ;)

Share This Page