茶树

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

//生日日期转换年龄

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  "茶树"  阅读(577)  评论(0编辑  收藏  举报