What's wrong with my first codes?

I got two methods to solve the the live time.it’s absolutely that my first codes are wrong , I have calculated my live time.I paste my two codes here, the first"i have been alive is …" is wrong, and the last “i have been alive …” is right. I want to know, what’s wrong with my first codes?
this is the codes below:
.#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *borntime = [NSDate dateWithTimeIntervalSinceReferenceDate: 1987-12-30];
NSDate *now = [NSDate date];
unsigned int seconds = [now timeIntervalSinceDate:borntime];
NSLog(@“I have born %u seconds”,seconds);
// insert code here…

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1987];
    [comps setMonth:12];
    [comps setDay:30];
    [comps setHour:0];
    [comps setMinute:0];
    [comps setSecond:0];
    NSCalendar *g = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *date0fBirth = [g dateFromComponents:comps];
    NSLog(@"Jack's date of birth is %@.",date0fBirth);
    NSLog(@"Hello, World!");
    NSDate *nowagain = [NSDate date];
    unsigned int secondsagain = [nowagain timeIntervalSinceDate:date0fBirth];
    NSLog(@"I have born %u seconds",secondsagain);
return 0;

and this is the part after running:
.2016-06-14 08:16:17.939 exercise14_7[454:11155]

I have born 487554232 seconds

2016-06-14 08:16:17.945 exercise14_7[454:11155] Jack’s date of birth is 1987-12-29 16:00:00 +0000.
2016-06-14 08:16:17.945 exercise14_7[454:11155] Hello, World!
2016-06-14 08:16:17.946 exercise14_7[454:11155]

I have born 898071377 seconds

Program ended with exit code: 0

The reason you are getting different results is easy to explain. The numeric expression 1987-12-30 (1987 minus 12 minus 30) is not the same as the string expression “1987-12-30”.

You probably wanted to use the string expression "1987-12-30" instead as the argument, but doing so would be wrong because NSDate class method dateWithTimeIntervalSinceReferenceDate: takes an NSTimeInterval as its argument.

For more information, see the NSDate class reference.

1 Like

wow, that’s the reason, got it, thank you!! my friend:grinning: