我正在为我的移动开发课程简介做作业。 我们必须得到自约会以来的秒数,并在几秒钟内给出这个人的年龄,我想扩展这个并显示该人的实际年龄。可以实现这一点(查看我的代码) 当日期设置为NEXT日,所以我的生日是明天,它显示的AGE不正确。 继承我的代码;
#import <Foundation/Foundation.h>
int main (int argc, char* argv[]) {
@autoreleasepool {
NSDateComponents* comps = [[NSDateComponents alloc] init];
[comps setYear: 2000];
[comps setMonth: 11];
[comps setDay: 25];
[comps setHour: 17];
[comps setMinute: 50];
[comps setSecond: 01];
NSCalendar* g = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate* dateOfBirth = [g dateFromComponents:comps];
NSDate* now = [NSDate date];
double secondsSince = [now timeIntervalSinceDate:dateOfBirth];
double actualAge = secondsSince / 60 / 60 / 24 / 365;
NSLog(@"\nI am %.9f Seconds Old \nI am Exactly %.9f Years Old %@", secondsSince, actualAge, now);
}
}
这是我的输出:
2014-11-24 13:35:23.547 secondsAlive[4978:44040]
I am 441661522.544027984 Seconds Old
I am Exactly 14.004995007 Years Old 2014-11-24 18:35:23 +0000
答案 0 :(得分:0)
一年有365.242天而不是365。