计算年龄
来源:互联网 发布:37热血战歌羽化数据 编辑:程序博客网 时间:2024/04/29 23:52
根据出生日期,计算年龄(距当前时间),调用:Age(20121202); 方法如下:
private void Age(string Date)
{
string RValue = string.Empty;
string NowDate = DateTime.Now.ToString("yyyyMMdd");
DateTime BrithDate = DateTime.Parse(Date.Insert(6, "-").Insert(4, "-"));
if (DateTime.Now.Year == BrithDate.Year) //年份相等
{
#region
if (DateTime.Now.Month == BrithDate.Month) //月份相等
{
RValue = (DateTime.Now.Day - BrithDate.Day).ToString() + "天";
}
else //月份不相等
{
if (DateTime.Now.Day >= BrithDate.Day)
{
RValue = (DateTime.Now.Month - BrithDate.Month).ToString() + "月";
}
else
{
RValue = (DateTime.Now.Month - BrithDate.Month - 1).ToString() + "月";
}
}
#endregion
}
else //年份不相等
{
#region
if (DateTime.Now.Month > BrithDate.Month)
{
if (DateTime.Now.Day >= BrithDate.Day)
RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年"+(DateTime.Now.Month-BrithDate.Month).ToString()+"月";
else
RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年" + (DateTime.Now.Month - BrithDate.Month-1).ToString() + "月";
}
else if (DateTime.Now.Month < BrithDate.Month)
{
if (DateTime.Now.Year - BrithDate.Year - 1 == 0)
{ //eg:出生日期:20111020,当前日期:20120820
if (DateTime.Now.Day >= BrithDate.Day)
{
RValue = (12 - BrithDate.Month + DateTime.Now.Month ).ToString() + "月";
}
else
{
RValue = (12 - BrithDate.Month + DateTime.Now.Month - 1).ToString() + "月";
}
}
else
{
RValue = (DateTime.Now.Year - BrithDate.Year - 1).ToString() + "年" + (12- BrithDate.Month +DateTime.Now.Month - 1).ToString() + "月";
}
}
else //年份不一样,月份相等
{
if (DateTime.Now.Day >= BrithDate.Day)
{
RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年";
}
else
{
if (DateTime.Now.Year - BrithDate.Year - 1 == 0)
{ //eg:出生日期:20111020,当前日期:20120820
if (DateTime.Now.Day >= BrithDate.Day)
{
RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年";
}
else
{
RValue = (12 - BrithDate.Month + DateTime.Now.Month - 1).ToString() + "月";
}
}
else
{
RValue = (DateTime.Now.Year - BrithDate.Year - 1).ToString() + "年";
}
}
}
#endregion
}
MessageBox.Show(RValue.ToString());
}
- 计算年龄
- 计算年龄
- 计算年龄
- 计算年龄
- 计算年龄
- 年龄计算
- 通过出生日期计算年龄
- 根据生日计算年龄
- 知道出生日期,计算年龄
- //根据日期计算年龄
- Jscript 计算年龄
- PHP 年龄计算函数
- 根据生日计算年龄
- 如何计算年龄
- 根据生日计算年龄
- 计算年龄代码
- php 计算年龄
- 根据生日计算年龄
- 用 C 语言编写 Windows 服务程序的五个步骤
- Hello TWaver Android
- 出现“entry index is larger than available symbols” <index ,total symbols> 的研究
- 使用Storm实现实时大数据分析
- 爸妈争吵小孩被吓出抽动症
- 计算年龄
- weblogic中文乱码问题总结
- combo+treePanel
- FLV视频封装格式详解
- spring2.0-2.5-3.0变化
- IOS塔防游戏《坦克对大炮》的开发设计记录
- 2013年让你不掉队的东西
- 跟linux编辑器vi一样强大--windows环境下的vim编辑器
- XML Web Service