using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SOH.Common { public class PubFunc { /// /// 根据身份证号获得出生日期 /// /// /// public static string GetCsrqBySfz(string sfzh) { //if (sfzh.Length != 18 && sfzh.Length != 15) if (sfzh.Length != 18 && sfzh.Length != 15) { return ""; } string temp = sfzh.Substring(6, 8); string y = temp.Substring(0, 4); string m = temp.Substring(4, 2); string d = temp.Substring(6, 2); string result = y + "-" + m + "-" + d; return result; } /// /// 根据身份证获取男女 /// /// /// public static string GetsexBysfz(string sfzh) { if (int.Parse(sfzh.Substring(14, 3)) % 2 == 0)//根据身份证号码标准格式确认男女,偶数为女,奇数为男 { return "女"; } else { return "男"; } } /// /// 根据身份证获取男女,0男,1女 /// /// /// public static short GetSexShortBysfz(string sfzh) { if (int.Parse(sfzh.Substring(14, 3)) % 2 == 0) { return 1; } else { return 0; } } /// /// 根据身份证号获取当前年龄 /// /// /// public static int GetNlBySfz(string sfzh) { string csrq = GetCsrqBySfz(sfzh); DateTime rq; bool ifdate=DateTime.TryParse(csrq, out rq); if (ifdate) { TimeSpan ts = DateTime.Now - rq; int temp = (int)ts.TotalDays; int years = temp / 365; return years; } else { return -1; } } } }