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

2019-06-19  本文已影响0人  执着的人请保持微笑

+(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];

}

上一篇下一篇

猜你喜欢

热点阅读