1 public class TimeTool 2 { 3 //根据出生年月计算 整数天 4 private static int GetAgeByBirthdate(DateTime birthdate) 5 { 6 DateTime now = DateTime.Now; 7 int age = now.Year - birthdate.Year; 8 if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day)) 9 {10 age--;11 }12 return age < 0 ? 0 : age;13 }
//根据出生年月计算 X岁或X月X天或X天14 public static string GetAgeByBirthday(DateTime birthday)15 {16 var currenttime = DateTime.Now;17 var diffTime = currenttime - birthday;18 if (diffTime.TotalDays >= 365)19 {20 //年龄计算21 return GetAgeByBirthdate(birthday).ToString() + "岁";22 }23 else24 {25 //个月计算26 var diffmonth = currenttime.Month - birthday.Month;27 var day = currenttime.Day - birthday.Day;28 if (day < 0)29 {30 diffmonth--;31 }32 if (diffmonth > 0)33 {34 DateTime newbirthday = birthday.AddMonths(diffmonth);35 day = (int)((currenttime - newbirthday).TotalDays);36 return diffmonth.ToString() + "个月" + (day == 0 ? "" : day.ToString() + "天");37 }38 else39 {40 //直接计算天41 return ((int)(diffTime.TotalDays)).ToString() + "天";42 }43 }44 45 }46 }
C# 根据出生年月 计算天数/计算X岁X月X天字符串亚马逊入驻、 品类管理、 亿恩网、 思考|疫情过后的几个长期变化、 2020年注册英国商标的详细资料、 亚马逊如何做好站外推广?亚马逊站外推广的方式有哪些?、 珠海金海滩有什么超好玩的娱乐项目呢?、 珠海中山亭自驾游怎么走?、 暑假去珠海金海滩玩合适吗?好玩吗?、
没有评论:
发表评论