第1个回答 2022-04-02
impala求身份证年龄,公式如下:
+(NSDateComponents*)componentsDateFrom:(NSDate*)birthDaytoDate:(NSDate*)nowDate{
if(!nowDate){
nowDate=[NSDatedate];
}
unsignedintunitFlags=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSCalendar*calendar=[NSCalendarcurrentCalendar];
NSDateComponents*date=[calendarcomponents:unitFlagsfromDate:birthDaytoDate:nowDateoptions:0];
if([dateyear]>0)
{
NSLog(@%@,[NSStringstringWithFormat:(@%ld-%ld-%ld),(long)[dateyear],(long)[datemonth],(long)[dateday]]);
}
elseif([datemonth]>0)
{
NSLog(@%@,[NSStringstringWithFormat:(@0-%ld-%ld),(long)[datemonth],(long)[dateday]]);
}
elseif([dateday]>0){
NSLog(@%@,[NSStringstringWithFormat:(@0-0-%ld),(long)[dateday]]);
}
else{
NSLog(@%@,@0-0-0);
}
returndate;
}