Birthday Time Change!

Hi Everyone,

I have a question about the comps setHour: part of my code.

In the code below I have 4 to represent 4am, however when I run the code I get an hour offset and the result of 3am… I have put the output below the code.

Can anyone help me understand this offset

I did read in a C programming book that setHour goes from 0 - 23.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear: 1976];
        [comps setMonth: 11];
        [comps setDay: 25];
        [comps setHour: 4];
        [comps setMinute: 45];
        [comps setSecond: 0];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [g dateFromComponents:comps];
        
        NSDate *seconds = [[NSDate alloc] init];
        double s = [seconds timeIntervalSinceDate:dateOfBirth]; //time in Seconds
        
        NSLog(@"My date of birth is: %@ \n\n", dateOfBirth);
        NSLog(@"Time difference between now and then in seconds is: %f", s);
        
    }
    return 0;
}

Output:
2014-10-10 22:04:07.432 Time_After_Time[1473:303] My date of birth is: 1976-11-25 03:45:00 +0000

2014-10-10 22:04:07.433 Time_After_Time[1473:303] Time difference between now and then in seconds is: 1195229947.428336
Program ended with exit code: 0

Cheers

JP

Use the NSDateFormatter to format the date and then print.

For example:

#import <Foundation/Foundation.h>

// Return the local-time string from the given date
NSString * StringFromDate (NSDate *);

int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        NSLog (@"Current time: %@", StringFromDate ([NSDate date]));
        
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setYear:1976];
        [dateComps setMonth:11];
        [dateComps setDay:25];
        [dateComps setHour:4];
        [dateComps setMinute:45];
        [dateComps setSecond:0];
        
        NSCalendar *newCalendarObj = [[NSCalendar alloc]
                                      initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [newCalendarObj dateFromComponents:dateComps];
        
        NSLog (@"Date of birth: %@", StringFromDate (dateOfBirth));

    }
    return 0;
}

// ---------------------------------------------------------------------------------------
//
// Courtesy of Pretty Function - www.pretty-function.org
//
// Return the local-time string from the given date
//
NSString * StringFromDate (NSDate *date)
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    
    [dateFormatter setLocale:[NSLocale currentLocale]];
    
    return [dateFormatter stringFromDate:date];
}

[Become a competent programmer faster than you can imagine: pretty-function.org]