C#根据身份证号码,计算生日、年龄、性别
朋友谈及身份证相关的信息,才了解到原来省份证号码中包含了年龄和性别。
这样在数据库中,就不必单独留字段存放它们了(不过,要根据具体情况来,要是读取频率较高,还是单独列出为好),这样顺带解决了年龄变更的问题。
程序仅仅为了实现这个功能,里面还是需要数据验证的,用户输入的信息,毕竟在猿类看来,都是“非法的”。废话不多说了,贴上我写的程序,还请路过的大神斧正:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace calculateAgeBirthdatSexDemo
{
public class Program
{
public static void Main(string[] args)
{
string identityCard = "32128119930718125X";//随便拼的,如有雷同,纯属搞怪哈
BirthdayAgeSex entity = new BirthdayAgeSex();
entity=GetBirthdayAgeSex(identityCard);
if (entity != null)
{
Console.WriteLine(entity.Birthday + "-----" + entity.Sex + "-----" + entity.Age);
}
Console.ReadLine();
} public static BirthdayAgeSex GetBirthdayAgeSex(string identityCard)
{
if (string.IsNullOrEmpty(identityCard))
{
return null;
}
else
{
if (identityCard.Length != && identityCard.Length != )//身份证号码只能为15位或18位其它不合法
{
return null;
}
} BirthdayAgeSex entity = new BirthdayAgeSex();
string strSex = string.Empty;
if (identityCard.Length == )//处理18位的身份证号码从号码中得到生日和性别代码
{
entity.Birthday = identityCard.Substring(, ) + "-" + identityCard.Substring(, ) + "-" + identityCard.Substring(, );
strSex = identityCard.Substring(, );
}
if (identityCard.Length == )
{
entity.Birthday = "" + identityCard.Substring(, ) + "-" + identityCard.Substring(, ) + "-" + identityCard.Substring(, );
strSex = identityCard.Substring(, );
} entity.Age = CalculateAge(entity.Birthday);//根据生日计算年龄
if (int.Parse(strSex) % == )//性别代码为偶数是女性奇数为男性
{
entity.Sex = "女";
}
else
{
entity.Sex = "男";
}
return entity;
} /// <summary>
/// 根据出生日期,计算精确的年龄
/// </summary>
/// <param name="birthDate">生日</param>
/// <returns></returns>
public static int CalculateAge(string birthDay)
{
DateTime birthDate=DateTime.Parse(birthDay);
DateTime nowDateTime=DateTime.Now;
int age = nowDateTime.Year - birthDate.Year;
//再考虑月、天的因素
if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
{
age--;
}
return age;
} /// <summary>
/// 定义 生日年龄性别 实体
/// </summary>
public class BirthdayAgeSex
{
public string Birthday { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}
}
(ps:多年前写的了,今天看了下,确实很水啊。。。。)
C#根据身份证号码,计算生日、年龄、性别的更多相关文章
- java 根据身份证号码获取出生日期、性别、年龄
1.情景展示 如何根据身份证号,计算出出生日期.性别.年龄? 2.解决方案 从网上找的别人的,因为并没有实际用到,所以并未对其优化! /** * 通过身份证号码获取出生日期.性别.年龄 * @pa ...
- [VBA]根据身份证号码计算年龄的Excel函数
是的,昨天刚发表了一篇和Excel自定义函数有关的博客,今天又一篇,有凑数的嫌疑.但是,保存知识和传播知识本来就是写博客的初衷,所以也并不多余. 如果不知道什么是Excel自定义函数,请移步这里[1] ...
- Excel自动从身份证中提取生日、性别、年龄
现在学生的身份证号已经全部都是18位的新一代身份证了,里面的数字都是有规律的.前6位数字是户籍所在地的代码,7-14位就是出生日期.第17位“2”代表的是性别,偶数为女性,奇数为男性.我们要做的就是把 ...
- python 根据生日计算年龄 sqlalchemy根据身份证号计算生日 性别
import datetime '): birth_d = datetime.datetime.strptime(birth_s, "%Y%m%d") today_d = date ...
- oracle根据身份证号码 计算年龄、性别
一.Oracle根据身份证判断性别: 女生身份证: 431382198103246985 男生身份证: 150921197208173492 SQL语句如下: select decode(mod ...
- php根据身份证号码计算年龄
代码如下 复制代码 <?php function getAgeByID($id){ //过了这年的生日才算多了1周岁 if(empty($id)) return ...
- mysql 中通过身份证号码计算年龄
SELECT DATE_FORMAT(NOW(), '%Y') - SUBSTRING( '换成对应身份证',7,4) AS age
- sql 根据身份证号码计算年龄
,), GETDATE()) / 365.25) from ConstructionInfo
- EXCEL计算根据当前时间和身份证号计算准确年龄
假设身份证号在A2单元格 =IF(MONTH(NOW())<MONTH(DATE(MID(A2,7,4),MID(A2,11,2),MID(A2,13,2))),INT(YEAR(NOW())- ...
随机推荐
- linux配置java环境变量(详细) -copy
一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车, ...
- [你必须知道的NOSQL系列]专题一:MongoDB快速入门
一.前言 现在越来越多的公司开始采用非关系数据库了,并且很多公司的面试都要求面试者有MongoDB的使用经验,至于非关系数据库与关系型数据库之间的区别大家可以自行百度.但是作为程序员的我们,既然大部分 ...
- C#中的线程一(委托中的异步)
C#中的线程一(委托中的异步) 一.同步委托 我们平时所用的委托以同步居多,我们编写一个方法和相关委托进行演示: publicdelegatevoid DoSomethingDelegate(stri ...
- debian(kali Linux) 安装net Core
debian(kali Linux) 安装net Core curl -sSL https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-previ ...
- Python黑客编程2 入门demo--zip暴力破解
Python黑客编程2 入门demo--zip暴力破解 上一篇文章,我们在Kali Linux中搭建了基本的Python开发环境,本篇文章为了拉近Python和大家的距离,我们写一个暴力破解zip包密 ...
- Flume概述和简单实例
Flume概述 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方( ...
- [stm32] SIM808模块之发短信\GPS\TCP\HTTP研究
SIM8008是四频模块,全球可用.含有TTL电平接口等接口,能够实现发短信.打电话.GPRS传输数据.GPS等功能.[正版资料请找beautifulzzzz·博客园] 一些细节: >> ...
- IOS 多线程05-OperationQueue 、GCD详解
注:本人是翻译过来,并且加上本人的一点见解. 1. 开始 目前在 iOS中有两套先进的同步 API 可供我们使用:操作队列OperationQueue和 GCD .其中 GCD 是基于 C 的底层 ...
- atitit.vod search doc.doc 点播系统搜索功能设计文档
atitit.vod search doc.doc 点播系统搜索功能设计文档 按键的enter事件1 Left rig事件1 Up down事件2 key_events.key_search = fu ...
- react5 事件 satate
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...