Solution to chapter 12


#1

    @autoreleasepool {

        // Create new data object from by birth date
        
        NSDateComponents *comps = [[NSDateComponents alloc]init];
        [comps setYear:1987];
        [comps setMonth:12];
        [comps setDay:21];
        [comps setHour:13];
        [comps setMinute:30];
        [comps setSecond:0];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        // My birthdate:
        
        NSDate *dateOfBirth = [g dateFromComponents:comps];
        
        NSLog ( @ "My date of birth is: %@", dateOfBirth);
        
        // Today right now:
        
        NSDate *now = [NSDate date];

        // The difference between now and the previous object created from my birth date
        
        double aliveSince = [now timeIntervalSinceDate: dateOfBirth];
        
        NSLog(@"I have been alive for %f seconds", aliveSince);
        
        
    }
    return 0;

Result:


2013-09-29 18:45:00.841 TimeAfterTime[3262:303] Your Date of Birth is: 1987-12-21 12:30:00 +0000
2013-09-29 18:45:00.842 TimeAfterTime[3262:303] I have been alive for 813384900.842436 seconds