生日年龄相互转换

//生日日期转换年龄

public static String GetPetAge(DateTime Petbirsday)
       {
           string nianling = "";
           string yue = "";
           string nian = "";

             //当前时间
           DateTime time_now = DateTime.Now;

            //生日日期
           DateTime petbsd = Convert.ToDateTime(Petbirsday.ToShortDateString());
              //获取日期差
           TimeSpan S = time_now - petbsd;

           //转换成相差几天
           int cha=Convert.ToInt32( S.TotalDays);

          //本月有多少天
           int MaxDay = ((TimeSpan)(time_now.AddMonths(1) - time_now)).Days;
          //本年有多少天

           int MaxYear = ((TimeSpan)(time_now.AddYears(1) - time_now)).Days;

           //相差几个月

           int cha_yue = Convert.ToInt32(cha / MaxDay);
           //相差几年

           int cha_nian = Convert.ToInt32(cha / MaxYear);
   //判断如果年份等于零年也就是说不到一岁
           if (cha_nian == 0)
           {

             //给月份赋值
               yue = Convert.ToString(cha_yue) + "个月";
           }
           else
           {

               //否则可能有月份有年份
               nian = Convert.ToString(cha_nian) + "岁";
               int yuenum = cha_yue - (Convert.ToInt32(cha_nian) * 12);
               if (yuenum != 0)
               {
                   yue = Convert.ToString(yuenum == 0 ? 0 : yuenum) + "个月";
               }
               }
           nianling = nian + yue;
           return nianling;
       }
       public static string BirthdayToAge(DateTime? petBirthday)
       {
           if (petBirthday == null)
           {
               return "";
           }
           else
           {
               try
               {
                   return GetPetAge(Convert.ToDateTime(petBirthday));
               }
               catch (Exception ex)
               {
                   LogHelper.WriteLog("转换年龄错误:",ex);
                   return "";
               }
           }
       }
       /// <summary>
       /// 字符串年龄转换为日期格式
       /// </summary>
       /// <param name="strAge">年龄字符串:2.5</param>
       /// <returns></returns>
       public static DateTime AgeToBirthday(string strAge)
       {
           try
           {
               double douAge = Convert.ToDouble(strAge);//年龄
               int intSection = Convert.ToInt32(Math.Floor(douAge));//整数部分
               double decSection = douAge - intSection;//小数部分
               DateTime nowDate=DateTime.Now;
               int nowYear = nowDate.Year;//当前年份
               int nowMonth = nowDate.Month;//当前月份
               int nowDay = nowDate.Day;//当前日
               int birYear = nowYear - intSection;//生日年份
               int birMonth = nowMonth;
               int month = Convert.ToInt32(Math.Floor(decSection * 12));
               if (nowMonth > month)
               {
                   birMonth = nowMonth - month;
               }
               else
               {
                   birMonth = nowMonth+12 - month;
                   birYear = birYear - 1;
               }
               DateTime birthday = new DateTime(birYear, birMonth, nowDay);
               return birthday;
           }
           catch (Exception ex)
           {
               LogHelper.WriteLog("年龄" + strAge + "转化出错:", ex);
               return new DateTime();
           }
       }

posted on 2016-03-29 16:41 "茶树" 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/LOVEJIEYING/p/5333512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值