iOS根据身份证号获取年龄生日性别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//根据身份证号获取生日
-(NSString *)birthdayStrFromIdentityCard:(NSString *)numberStr
{
NSMutableString *result = [NSMutableString stringWithCapacity:0];
NSString *year = nil;
NSString *month = nil;

BOOL isAllNumber = YES;
NSString *day = nil;
if([numberStr length]<14)
return result;

//**截取前14位
NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(0, 13)];

//**检测前14位否全都是数字;
const char *str = [fontNumer UTF8String];
const char *p = str;
while (*p!='\0') {
if(!(*p>='0'&&*p<='9'))
isAllNumber = NO;
p++;
}
if(!isAllNumber)
return result;

year = [numberStr substringWithRange:NSMakeRange(6, 4)];
month = [numberStr substringWithRange:NSMakeRange(10, 2)];
day = [numberStr substringWithRange:NSMakeRange(12,2)];

[result appendString:year];
[result appendString:@"-"];
[result appendString:month];
[result appendString:@"-"];
[result appendString:day];
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//根据身份证号性别
-(NSString *)getIdentityCardSex:(NSString *)numberStr
{
int sexInt=[[numberStr substringWithRange:NSMakeRange(16,1)] intValue];

if(sexInt%2!=0)
{
return @"1";
}
else
{
return @"2";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//根据省份证号获取年龄
-(NSString *)getIdentityCardAge:(NSString *)numberStr
{

NSDateFormatter *formatterTow = [[NSDateFormatter alloc]init];
[formatterTow setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *bsyDate = [formatterTow dateFromString:[self birthdayStrFromIdentityCard:numberStr]];

NSTimeInterval dateDiff = [bsyDate timeIntervalSinceNow];

int age = trunc(dateDiff/(60*60*24))/365;

return [NSString stringWithFormat:@"%d",-age];
}

15位身份证获取生日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-(NSString *)birthdayStrFromIdentityCard:(NSString *)numberStr
{
NSMutableString *result = [NSMutableString stringWithCapacity:0];
NSString *year = nil;
NSString *month = nil;

BOOL isAllNumber = YES;

NSString *day = nil;
if([numberStr length]<14)
return result;

// //**截取前14位
// NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(0, 13)];
//
// //**检测前14位否全都是数字;
// const char *str = [fontNumer UTF8String];
// const char *p = str;
// while (*p!='\0') {
// if(!(*p>='0'&&*p<='9'))
// isAllNumber = NO;
// p++;
// }
// if(!isAllNumber)
// return result;
//
// year = [numberStr substringWithRange:NSMakeRange(6, 4)];
// month = [numberStr substringWithRange:NSMakeRange(10, 2)];
// day = [numberStr substringWithRange:NSMakeRange(12,2)];
//
// [result appendString:year];
// [result appendString:@"-"];
// [result appendString:month];
// [result appendString:@"-"];
// [result appendString:day];
// return result;

//**截取前12位
NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(0, 11)];

//**检测前12位否全都是数字;
const char *str = [fontNumer UTF8String];
const char *p = str;
while (*p!='\0') {
if(!(*p>='0'&&*p<='9'))
isAllNumber = NO;
p++;
}
if(!isAllNumber)
return result;


year = [NSString stringWithFormat:@"19%@",[numberStr substringWithRange:NSMakeRange(6, 2)]];
month = [numberStr substringWithRange:NSMakeRange(8, 2)];
day = [numberStr substringWithRange:NSMakeRange(10,2)];


[result appendString:year];
[result appendString:@"-"];
[result appendString:month];
[result appendString:@"-"];
[result appendString:day];
return result;

}