zoukankan      html  css  js  c++  java
  • C# 根据生日获取年龄

    C# 根据生日获取年龄

        根据生日计算出准确的年龄,不等于0时,返回的是岁,等于0时,返回的是天(以‘-’来区分)

    public static string GetAgeByBirth(string Birthdate)
        {
            string ages = string.Empty;
            try
            {
                //年龄格式化
                DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
                dtFormat.ShortDatePattern = "yyyy-MM-dd";
                DateTime dt = Convert.ToDateTime(Birthdate, dtFormat);
                int age = DateTime.Now.Year - dt.Year;
                if (DateTime.Now.Month < dt.Month || (DateTime.Now.Month == dt.Month && DateTime.Now.Day < dt.Day)) age--;
                TimeSpan ts = DateTime.Now - dt;
                ages = age == 0 ? "-" + ts.Days : age.ToString();
            }
            catch(Exception ex)
            {
                BuildLogFile(ex.Message);
            }
            
            return ages;
        }
  • 相关阅读:
    selenium 难定位元素、时间插件
    列表生成式
    三元表达式
    监控日志
    非空即真
    深拷贝浅拷贝
    元组
    list字典嵌套
    2021
    布尔类型
  • 原文地址:https://www.cnblogs.com/ocean-wang/p/10979689.html
Copyright © 2011-2022 走看看