从Age看到DateOFBirth

rav*_*avi 2 iphone android objective-c

是否有任何公式可以直接从TextField的年,月和日中找到DOB?InShort把21年6个月的16天放在文本文件中,返回计算DOB?

Vig*_*esh 5

在iphone中你没有直接的方法,你怎么能自己写.

步骤1:从年,月,日创建NSDate对象.

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setYear:year];
[comp setMonth:month];
[comp setDay:day];
 NSDate *birthdate = [[NSCalendar currentCalendar] dateFromComponents:comp];
[comp release];
Run Code Online (Sandbox Code Playgroud)

第2步:从今天开始查找间隔.

NSTimeInterval dateDiff = [birthDate timeIntervalSinceNow];
int age=trunc(dateDiff/(60*60*24))/365;
Run Code Online (Sandbox Code Playgroud)

编辑......

    NSDateComponents *comp = [[NSDateComponents alloc] init];
    [comp setYear:-year];
    [comp setMonth:-month];
    [comp setDay:-day];
     NSDate *birthdate = [[NSCalendar currentCalendar]dateByAddingComponents:comp toDate:[NSDate date] options:0];;
    [comp release];
Run Code Online (Sandbox Code Playgroud)