计算年龄

- (void)viewDidLoad {

    [superviewDidLoad];

    

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    [dateFormattersetDateFormat:@"yyyy-MM-dd"];

    

   NSDate* birth ;

   NSNumber* age ;

    

    //假设现在是20151121

    

    //月小于今日

    {

        birth = [dateFormatterdateFromString:@"2014-10-20"];

        //日小于今日  ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:1",age);

        

        

        birth = [dateFormatterdateFromString:@"2014-10-21"];

        //日等于今日 ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:1",age);

        

        birth = [dateFormatterdateFromString:@"2014-10-22"];

        //日大于今日  ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:1",age);

    }


    

    //月等于今日

    {

        birth = [dateFormatterdateFromString:@"2014-11-20"];

        // 日小于今日 ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:1",age);

        

        birth = [dateFormatterdateFromString:@"2014-11-21"];

       //日等于今日;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:1",age);

        

        birth = [dateFormatterdateFromString:@"2014-11-22"];

        //日大于今日 ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:0",age);

    }

    

    //月大于今日

    {

        birth = [dateFormatterdateFromString:@"2014-12-20"];

        //日小于今日 ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:0",age);

        

        birth = [dateFormatterdateFromString:@"2014-12-21"];

       //日等于今日;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:0",age);

        

        birth = [dateFormatterdateFromString:@"2014-12-22"];

        //日大于今日  ;

        age = [ViewControllercomputeAgeWithBirth:birth];

       NSLog(@"age:%@  应是:0",age);

    }

    

}



//由生日计算年龄

+(NSNumber*)computeAgeWithBirth:(NSDate*)birth{

    

   /*

     法律上计算年龄:如果199579日生,那么从2009710日起你就已经14岁,到2010710开始15岁。

     */

    

   NSCalendar* calendar = [NSCalendarcurrentCalendar];

    

   NSInteger birthYear  = 0 ,birthMonth  = 0, birthDay  = 0;

   NSInteger nowYear = 0 ,nowMonth = 0, nowDay = 0;

    [calendargetEra:NULLyear:&birthYear month:&birthMonthday:&birthDay fromDate:birth];

    [calendargetEra:NULLyear:&nowYear month:&nowMonthday:&nowDay fromDate:[NSDatedate]];

    

    //只为测试

    {

        nowYear =2015;

        nowMonth =11;

        nowDay =21;

    }

    

   if(nowYear<birthYear){

       return nil;

    }

    

   NSInteger age = nowYear - birthYear;

   if(nowMonth<birthMonth

       || (nowMonth == birthMonth && nowDay<birthDay)){

        age -=1;

    }

    

   return @(age);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值