My hour of birth is off when creating a new date object?


#1

Hi guys,
So does anyone know why the time is off when I input this code: [code]
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1969];
[comps setMonth:4];
[comps setDay:30];
[comps setHour:13];
[comps setMinute:10];
[comps setSecond:0];

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    NSLog(@"I was born on %@", dateOfBirth);[/code]

That I receive this output:

It should be “I was born on 1969-04-30 13:10:00 +0000”, no? OR does this have to do with UTC, and if I were coding a program where the date is important, would I have to compensate by inputting 6 instead of 13 to set the hour to 1:00 PM?

Thanks!


#2

It does have to do with UTC. I had the same problem. Try setting the calendar timezone

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1969];
[comps setMonth:4];
[comps setDay:30];
[comps setHour:13];
[comps setMinute:10];
[comps setSecond:0];

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [g setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    NSLog(@"I was born on %@", dateOfBirth);