2019-06-19计算年龄的方法

+(NSString*)dateToOld:(NSDate*)bornDate{

    // 出生日期转换 年月日

    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:bornDate];

    NSIntegerbrithDateYear  = [components1year];

    NSIntegerbrithDateDay  = [components1day];

    NSIntegerbrithDateMonth = [components1month];


    // 获取系统当前 年月日

    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

    NSIntegercurrentDateYear  = [components2year];

    NSIntegercurrentDateDay  = [components2day];

    NSIntegercurrentDateMonth = [components2month];


    // 计算年龄

    NSInteger age = currentDateYear - brithDateYear -1;

    if((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {

       age++;

    }


    return [NSString stringWithFormat:@"%ld",(long)age];

}

你可能感兴趣的:(2019-06-19计算年龄的方法)