//根据身份证号性别
-(NSString*)getIdentityCardSex:(NSString*)numberStr
{
int sexInt=[[numberStr substringWithRange:NSMakeRange(16,1)]intValue];
if(sexInt%2!=0)
{
return@"男";
}
else
{
return@"女";
}
}
//根据省份证号获取年龄
-(NSString*)getIdentityCardAge:(NSString*)numberStr
{
NSDateFormatter*formatterTow = [[NSDateFormatteralloc]init];
[formatter TowsetDateFormat:@"yyyy-MM-dd"];
NSString*dateStr = [self birthdayStrFromIdentityCard:numberStr];
NSDate*date = [formatter TowdateFromString:dateStr];
NSTimeInterval dateDiff = [date timeIntervalSinceNow];
int age =trunc(dateDiff/(60*60*24))/365;
return[NSString stringWithFormat:@"%d",-age];
}
-(NSString*)birthdayStrFromIdentityCard:(NSString*)numberStr
{
NSMutableString*result = [NSMutableString stringWithCapacity:0];
NSString*year =nil;
NSString*month =nil;
BOOLisAllNumber =YES;
NSString*day =nil;
if([numberStr length]<14)
return result;
//**截取前14位
NSString*fontNumer = [number StrsubstringWithRange:NSMakeRange(0,13)];
//**检测前14位否全都是数字;
constchar*str = [fontNumer UTF8String];
constchar*p = str;
while(*p!='\0') {
if(!(*p>='0'&&*p<='9'))
isAllNumber =NO;
p++;
}
if(!isAllNumber)
returnresult;
year = [numberStrsubstringWithRange:NSMakeRange(6,4)];
month = [numberStrsubstringWithRange:NSMakeRange(10,2)];
day = [numberStrsubstringWithRange:NSMakeRange(12,2)];
[resultappendString:year];
[resultappendString:@"-"];
[resultappendString:month];
[resultappendString:@"-"];
[resultappendString:day];
returnresult;
}
- (NSInteger)ageWithDateOfBirth:(NSDate*)date;
{
//出生日期转换年月日
NSDateComponents*components1 = [[NSCalendarcurrentCalendar]components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnitfromDate:date];
NSIntegerbrithDateYear= [components1year];
NSIntegerbrithDateDay= [components1day];
NSIntegerbrithDateMonth = [components1month];
//获取系统当前年月日
NSDateComponents*components2 = [[NSCalendarcurrentCalendar]components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnitfromDate:[NSDatedate]];
NSIntegercurrentDateYear= [components2year];
NSIntegercurrentDateDay= [components2day];
NSIntegercurrentDateMonth = [components2month];
//计算年龄
NSIntegeriAge = currentDateYear - brithDateYear -1;
if((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
iAge++;
}
returniAge;
}