/**

从身份证上获取年龄 18位身份证

*/

-(NSString *)getIdentityCardAge:(NSString *)numberStr

{

NSDateFormatter *formatterTow = [[NSDateFormatter alloc]init];

[formatterTow setDateFormat:@"yyyy-MM-dd"];

NSDate *bsyDate = [formatterTow dateFromString:[self birthdayStrFromIdentityCard:numberStr]];

NSTimeInterval dateDiff = [bsyDate 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;

BOOL isAllNumber = YES;

NSString *day = nil;

if([numberStr length]<18)

return result;

//**从第6位开始 截取8个数

NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(6, 8)];

//**检测前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(8, 2)]];

//    NSLog(@"year ==%@",year);

month = [numberStr substringWithRange:NSMakeRange(10, 2)];

//    NSLog(@"month ==%@",month);

day = [numberStr substringWithRange:NSMakeRange(12,2)];

//    NSLog(@"day==%@",day);

[result appendString:year];

[result appendString:@"-"];

[result appendString:month];

[result appendString:@"-"];

[result appendString:day];

//    NSLog(@"result===%@",result);

return result;

}

/**

*  从身份证上获取性别

*/

-(NSString *)getIdentityCardSex:(NSString *)numberStr

{

NSString *sex = @"";

//获取18位 二代身份证  性别

if (numberStr.length==18)

{

int sexInt=[[numberStr substringWithRange:NSMakeRange(16,1)] intValue];

if(sexInt%2!=0)

{

NSLog(@"1");

sex = @"男";

}

else

{

NSLog(@"2");

sex = @"女";

}

}

//  获取15位 一代身份证  性别

if (numberStr.length==15)

{

int sexInt=[[numberStr substringWithRange:NSMakeRange(14,1)] intValue];

if(sexInt%2!=0)

{

NSLog(@"1");

sex = @"男";

}

else

{

NSLog(@"2");

sex = @"女";

}

}

return sex;

}

IOS 根据身份证号码获取 年龄 生日 性别的更多相关文章

  1. java 根据身份证号码获取出生日期、性别、年龄

      1.情景展示 如何根据身份证号,计算出出生日期.性别.年龄? 2.解决方案 从网上找的别人的,因为并没有实际用到,所以并未对其优化! /** * 通过身份证号码获取出生日期.性别.年龄 * @pa ...

  2. SQL 根据身份证号码获取年龄的函数

    在数据库的运用过程中,我们时常会碰到根据身份证号码来获取当前的年龄,今天我在这里写了一个函数,就是关于获取年龄的 create or replace function FUNC_COMPARE_SFZ ...

  3. oracle根据身份证号码 计算年龄、性别

    一.Oracle根据身份证判断性别: 女生身份证: 431382198103246985 男生身份证: 150921197208173492 SQL语句如下:   select decode(mod ...

  4. JAVA通过身份证号码获取出生日期、年龄、性别

    JAVA验证身份证号码是否正确:https://www.cnblogs.com/pxblog/p/12038278.html /** * 通过身份证号码获取出生日期(birthday).年龄(age) ...

  5. php 身份证号码获取星座和生肖

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,php用身份证号码获取星座和生肖的方法,一个简单的php实例,从身份证号码中取得星座与生肖信息,有兴趣的朋友参考研究下吧.本 ...

  6. 【代码笔记】iOS-传身份证号码可返回生日字符串

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NS ...

  7. PHP提取身份证号码中的生日并验证是否成年的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...

  8. php提取身份证号码中的生日日期以及验证是否为未成年人的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...

  9. C#根据身份证号码,计算生日、年龄、性别

    朋友谈及身份证相关的信息,才了解到原来省份证号码中包含了年龄和性别. 这样在数据库中,就不必单独留字段存放它们了(不过,要根据具体情况来,要是读取频率较高,还是单独列出为好),这样顺带解决了年龄变更的 ...

随机推荐

  1. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  2. Oracle:exp导出exp-00091问题

    今天导出一数据库数据,发现EXP-00091问题: 连接到: Oracle Database 10g Enterprise Edition Release - Production With the ...

  3. codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)

    题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...

  4. UVA-11078(水题)

    题意: 给一个序列,找两个整数a[i],a[j]使得a[i]-a[j]最大; 思路: 从前往后扫一遍;水题; AC代码: #include <bits/stdc++.h> /* #incl ...

  5. 「BZOJ3438」小M的作物(最小割

    3438: 小M的作物 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1891  Solved: 801[Submit][Status][Discus ...

  6. bzoj 4289 TAX —— 点边转化

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 把边转化成点,同一个原有点相连的边中,边权小的向大的连差值的边,大的向小的连0的边: ...

  7. B. Color the Fence

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 11.5NOIP模拟赛

    /* 唉,那个第二种策略应该是没用吧. 为什么我列出式子最优策略跟第二种策略有关??为什么我写了写分布列还是跟第二种策略有关??为什么数学还没学到期望的孩子们都A了? 唉.人生啊. 0.5h后 我好像 ...

  9. 洛谷P4841 城市规划(多项式求逆)

    传送门 这题太珂怕了……如果是我的话完全想不出来…… 题解 //minamoto #include<iostream> #include<cstdio> #include< ...

  10. EasyUI设置Layout自适应浏览器宽度和高度

    //设置自适应浏览器宽度和高度 function setLayoutHeight() { var height = $(window).height() - 20; $("#main_lay ...