计算年龄
????????????根據出生日期,計算年齡(距當前時間),調用: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());
??????? }
?
總結
- 上一篇: 微信公众号运营,这些运营技巧不能少
- 下一篇: 【Windows】32位和64位区别