mysql怎么 计算周岁_C#计算周岁 - osc_h7d57ub2的个人空间 - OSCHINA - 中文开源技术交流社区...

///

/// 计算年龄字符串(周岁)

/// 默认返回:xx岁xx月xx日

///

/// 第1个日期参数

/// 第2个日期参数

/// 返回字符串的格式,默认为:{0}岁{1}月{2}日

private static string CalculateAgeString(DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)

{

//判断时间段是否为正。若为负,调换两个时间点的位置。

if (System.DateTime.Compare(p_FirstDateTime, p_SecondDateTime) > 0)

{

System.DateTime stmpDateTime = p_FirstDateTime;

p_FirstDateTime = p_SecondDateTime;

p_SecondDateTime = stmpDateTime;

}

//判断返回字符串的格式。若为空,则给默认值:{0}岁{1}月{2}日

if (string.IsNullOrEmpty(p_ReturnFormat)) p_ReturnFormat = "{0}岁{1}月{2}日";

//定义:年、月、日

int year, month, day;

//计算:天

day = p_SecondDateTime.Day - p_FirstDateTime.Day;

if (day < 0)

{

day += System.DateTime.DaysInMonth(p_FirstDateTime.Year, p_FirstDateTime.Month);

p_FirstDateTime = p_FirstDateTime.AddMonths(1);

}

//计算:月

month = p_SecondDateTime.Month - p_FirstDateTime.Month;

if (month < 0)

{

month += 12;

p_FirstDateTime = p_FirstDateTime.AddYears(1);

}

//计算:年

year = p_SecondDateTime.Year - p_FirstDateTime.Year;

//返回格式化后的结果

return string.Format(p_ReturnFormat, year, month, day);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值