zoukankan      html  css  js  c++  java
  • C#根据出生日期和当前日期计算精确年龄

    C#根据出生日期和当前日期计算精确年龄
    更多 0
    c#.net基础


    public static string GetAge(DateTime dtBirthday, DateTime dtNow)
    {
    string strAge = string.Empty; // 年龄的字符串表示
    int intYear = 0; // 岁
    int intMonth = 0; // 月
    int intDay = 0; // 天

    // 如果没有设定出生日期, 返回空
    if (DataType.DateTime_IsNull(ref dtBirthday) == true)
    {
    return string.Empty;
    }

    // 计算天数
    intDay = dtNow.Day - dtBirthday.Day;
    if (intDay < 0)
    {
    dtNow = dtNow.AddMonths(-1);
    intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
    }

    // 计算月数
    intMonth = dtNow.Month - dtBirthday.Month;
    if (intMonth < 0)
    {
    intMonth += 12;
    dtNow = dtNow.AddYears(-1);
    }

    // 计算年数
    intYear = dtNow.Year - dtBirthday.Year;

    // 格式化年龄输出
    if (intYear >= 1) // 年份输出
    {
    strAge = intYear.ToString() + "岁";
    }

    if (intMonth > 0 && intYear <= 5) // 五岁以下可以输出月数
    {
    strAge += intMonth.ToString() + "月";
    }

    if (intDay >= 0 && intYear < 1) // 一岁以下可以输出天数
    {
    if (strAge.Length == 0 || intDay > 0)
    {
    strAge += intDay.ToString() + "日";
    }
    }

    return strAge;
    }

  • 相关阅读:
    Mybatis3详解(一)----Mybatis的介绍
    【k8s】svc-sessionAffinityConfig
    【k8s】svc-sessionAffinity
    【k8s】svc-selector
    【k8s】svc-publishNotReadyAddresses
    【k8s】svc-ports
    【k8s】svc-externalIPs
    【k8s】svc-clusterIPs
    【k8s】svc-clusterIP
    【k8s】Service
  • 原文地址:https://www.cnblogs.com/Echo529/p/6382346.html
Copyright © 2011-2022 走看看