tijian_jichuang/Code/SOH.Kernel/Common/PubFunc.cs

86 lines
2.3 KiB
C#
Raw Normal View History

2025-02-20 11:54:48 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SOH.Common
{
public class PubFunc
{
/// <summary>
/// 根据身份证号获得出生日期
/// </summary>
/// <param name="sfzh"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据身份证获取男女
/// </summary>
/// <param name="sfzh"></param>
/// <returns></returns>
public static string GetsexBysfz(string sfzh)
{
if (int.Parse(sfzh.Substring(14, 3)) % 2 == 0)//根据身份证号码标准格式确认男女,偶数为女,奇数为男
{
return "女";
}
else
{
return "男";
}
}
/// <summary>
/// 根据身份证获取男女,0男1女
/// </summary>
/// <param name="sfzh"></param>
/// <returns></returns>
public static short GetSexShortBysfz(string sfzh)
{
if (int.Parse(sfzh.Substring(14, 3)) % 2 == 0)
{
return 1;
}
else
{
return 0;
}
}
/// <summary>
/// 根据身份证号获取当前年龄
/// </summary>
/// <param name="sfzh"></param>
/// <returns></returns>
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;
}
}
}
}