美文网首页
2019-06-19计算年龄的方法

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

作者: 执着的人请保持微笑 | 来源:发表于2019-06-19 17:41 被阅读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];

    }

    相关文章

      网友评论

          本文标题:2019-06-19计算年龄的方法

          本文链接:https://www.haomeiwen.com/subject/vsxlqctx.html