1.精确计算年龄
/// <summary>
/// 精确计算年龄
/// </summary>
/// <param name="currentYear">当前年度</param>
/// <param name="currentMonth">当前月度</param>
/// <param name="currentDay">当前日度</param>
/// <param name="birthYear">出生年度</param>
/// <param name="birthMonth">出生月度</param>
/// <param name="birthDay">出生日度</param>
public static int Age(int currentYear,int currentMonth,int currentDay,int birthYear,int birthMonth,int birthDay)
{
int a = currentYear - birthYear;//当前年度减去出生年度
int b = currentMonth - birthMonth;//当前月减去出生月
int c = currentDay - birthDay;//当前日减去出生日
//如果月度得到零,则未到生日减一
if (b < 0)
{
a -= 1;
}
else if (b == 0&&c < 0)//如果当前月等于表示生日就在这个月,如果当前日小于0则未到生日
{
a -= 1;
}
//如果以上条件全部不于0则生日已到
return a;
}
3. 通过以上方法实现扩展
/// <summary>
/// 精确计算时间
/// </summary>
/// <param name="currentTime">当前日期格式:20000101</param>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(int currentTime,int birthTime)
{
int a = SplitInt(currentTime, 0, 3);
int b = SplitInt(currentTime, 4, 5);
int c = SplitInt(currentTime, 6, 7);
int aa= SplitInt(birthTime, 0, 3);
int bb = SplitInt(birthTime, 4, 5);
int cc = SplitInt(birthTime, 6, 7);
return Age(a, b, c, aa, bb, cc);
}
/// <summary>
/// 精确计算时间
/// </summary>
/// <param name="currentTime">当前日期格式:20000101</param>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(string currentTime, string birthTime)
{
int a = SplitStringToInt(currentTime, 0, 3);
int b = SplitStringToInt(currentTime, 4, 5);
int c = SplitStringToInt(currentTime, 6, 7);
int aa = SplitStringToInt(birthTime, 0, 3);
int bb = SplitStringToInt(birthTime, 4, 5);
int cc = SplitStringToInt(birthTime, 6, 7);
return Age(a, b, c, aa, bb, cc);
}
/// <summary>
/// 精确计算时间
/// </summary>
/// <param name="currentTime">当前日期格式:20000101</param>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(int currentTime, string birthTime)
{
int a = SplitInt(currentTime, 0, 3);
int b = SplitInt(currentTime, 4, 5);
int c = SplitInt(currentTime, 6, 7);
int aa = SplitStringToInt(birthTime, 0, 3);
int bb = SplitStringToInt(birthTime, 4, 5);
int cc = SplitStringToInt(birthTime, 6, 7);
return Age(a, b, c, aa, bb, cc);
}
/// <summary>
/// 精确计算时间
/// </summary>
/// <param name="currentTime">当前日期格式:20000101</param>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(string currentTime, int birthTime)
{
int a = SplitStringToInt(currentTime, 0, 3);
int b = SplitStringToInt(currentTime, 4, 5);
int c = SplitStringToInt(currentTime, 6, 7);
int aa = SplitInt(birthTime, 0, 3);
int bb = SplitInt(birthTime, 4, 5);
int cc = SplitInt(birthTime, 6, 7);
return Age(a, b, c, aa, bb, cc);
}
/// <summary>
/// 使用系统时间计算出生年龄
/// </summary>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(int birthTime)
{
int a=int.Parse(DateTime.Now.ToString("yyyyMMdd"));
return Age(a, birthTime);
}
/// <summary>
/// 使用系统时间计算出生年龄
/// </summary>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <returns></returns>
public static int Age(string birthTime)
{
int a = int.Parse(DateTime.Now.ToString("yyMMdd"));
return Age(a, birthTime);
}
/// <summary>
/// 使用系统时间计算出生年龄
/// </summary>
/// <param name="birthTime">出生日期格式:20000101</param>
/// <param name="split">含分割符号</param>
/// <returns></returns>
public static int AgeSplit(string birthTime,params char[] split)
{
int a = int.Parse(DateTime.Now.ToString("yyMMdd"));
string[] strs = birthTime.Split(split);
string str="";
foreach(var s in strs)
{
str += s;
}
return Age(a, str);
}
/// <summary>
/// 计算年龄。计算公民身份证出生年龄。
/// </summary>
///<param name="currentTime">当前时间格式:20000101</param>
/// <param name="id">身份证号码</param>
/// <returns></returns>
public static int AgeId(int currentTime,long id)
{
int idtime = SplitLong(id, 6, 13);
return Age(currentTime, idtime);
}
/// <summary>
/// 计算年龄。计算公民身份证出生年龄。
/// </summary>
///<param name="currentTime">当前时间格式:20000101</param>
/// <param name="id">身份证号码</param>
/// <returns></returns>
public static int AgeId(int currentTime, string id)
{
int idtime = SplitStringToInt(id, 6, 13);
return Age(currentTime, idtime);
}
/// <summary>
/// 计算年龄。计算公民身份证出生年龄。
/// </summary>
///<param name="currentTime">当前时间格式:20000101</param>
/// <param name="id">身份证号码</param>
/// <returns></returns>
public static int AgeId(string currentTime, string id)
{
int idtime = SplitStringToInt(id, 6, 13);
return Age(currentTime, idtime);
}
/// <summary>
/// 计算年龄。计算公民身份证出生年龄。
/// </summary>
///<param name="currentTime">当前时间格式:20000101</param>
/// <param name="id">身份证号码</param>
/// <returns></returns>
public static int AgeId(string currentTime, long id)
{
int idtime = SplitLong(id, 6, 13);
return Age(currentTime, idtime.ToString());
}
/// <summary>
/// 使用系统时间计算公民身份证出生年龄
/// </summary>
/// <param name="id">身份证号码</param>
/// <returns></returns>
public static int AgeId(long id)
{
int a = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int idtime = SplitLong(id, 6, 13);
return Age(a, idtime);
}
/// <summary>
/// 使用系统时间计算公民身份证出生年龄
/// </summary>
/// <param name="birthTime">出生日期</param>
/// <returns></returns>
public static int AgeId(string id)
{
int a = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int idtime = SplitStringToInt(id, 6, 13);
return Age(a, idtime);
}
/// <summary>
/// 分割字符串从开始到结束字符,并转换成int类型
/// </summary>
/// <param name="value">分割的字符</param>
/// <param name="start">开始分割</param>
/// <param name="end">结束分割</param>
/// <returns></returns>
public static int SplitStringToInt(string value, int start, int end)
{
string a = "";
for (int i = start; i <= end; i++)
{
a += value[i];
}
return int.Parse(a);
}
/// <summary>
/// 分割字符串从开始到结束字符
/// </summary>
/// <param name="value">分割的字符</param>
/// <param name="start">开始分割</param>
/// <param name="end">结束分割</param>
/// <returns></returns>
public static string SplitString(string value, int start, int end)
{
string a = "";
for (int i = start; i <= end; i++)
{
a += value[i];
}
return a;
}
/// <summary>
/// 分割Int类型从开始到结束
/// </summary>
/// <param name="value">分割的整数</param>
/// <param name="start">开始分割</param>
/// <param name="end">结束分割</param>
/// <returns></returns>
public static int SplitInt(int value, int start, int end)
{
string a = "";
for (int i = start; i <= end; i++)
{
a += value.ToString()[i];
}
return int.Parse(a);
}
/// <summary>
/// 分割Int类型从开始到结束
/// </summary>
/// <param name="value">分割的整数</param>
/// <param name="start">开始分割</param>
/// <param name="end">结束分割</param>
/// <returns></returns>
public static int SplitLong(long value, int start, int end)
{
string a = "";
for (int i = start; i <= end; i++)
{
a += value.ToString()[i];
}
return int.Parse(a);
}
/// <summary>
/// 分割Int类型从开始到结束,并转换成string类型
/// </summary>
/// <param name="value">分割的整数</param>
/// <param name="start">开始分割</param>
/// <param name="end">结束分割</param>
/// <returns></returns>
public static string SplitIntToString(int value, int start, int end)
{
string a = "";
for (int i = start; i <= end; i++)
{
a += value.ToString()[i];
}
return a;
}