zoukankan      html  css  js  c++  java
  • 根据身份号 计算生日 年龄 性别

    /**
     
     从身份证上获取年龄 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!='') {
            if(!(*p>='0'&&*p<='9'))
                isAllNumber = NO;
            p++;
        }
        
        if(!isAllNumber)
            return result;
        year = [NSString stringWithFormat:@"19%@",[numberStr substringWithRange:NSMakeRange(8, 2)]];
        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;
    }
    
    //根据身份证号性别
    -(NSString *)getIdentityCardSex:(NSString *)numberStr
    {
        int sexInt=[[numberStr substringWithRange:NSMakeRange(16,1)] intValue];
        
        if(sexInt%2!=0)
        {
            //
            return @"1";
        }
        else
        {
            //
            return @"2";
        }
    }
  • 相关阅读:
    删除字符串组中相同元素,并删除值为空的元素 (转载,笔记)
    获取操作系统语言
    .net 传递中文参数解决办法
    古怪问题:vs2003程序 在繁体平台下控件位置发生变化
    Godaddy邮箱C#发送邮件设置
    无法显示隐藏文件的解决方法
    虚拟机文件
    sql 2000 修复问题
    看QQ是否在线
    sql 知识摘录
  • 原文地址:https://www.cnblogs.com/dujiahong/p/7428999.html
Copyright © 2011-2022 走看看