zoukankan      html  css  js  c++  java
  • C#计算周岁

    /// <summary>
    /// 计算年龄字符串(周岁)
    /// 默认返回:xx岁xx月xx日
    /// </summary>
    /// <param name="p_FirstDateTime">第1个日期参数</param>
    /// <param name="p_SecondDateTime">第2个日期参数</param>
    /// <param name="p_Format">返回字符串的格式,默认为:{0}岁{1}月{2}日</param>
    private static string CalculateAgeString(DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)
    {
      //判断时间段是否为正。若为负,调换两个时间点的位置。
      if (System.DateTime.Compare(p_FirstDateTime, p_SecondDateTime) > 0)
      {
        System.DateTime stmpDateTime = p_FirstDateTime;
        p_FirstDateTime = p_SecondDateTime;
        p_SecondDateTime = stmpDateTime;
      }

      //判断返回字符串的格式。若为空,则给默认值:{0}岁{1}月{2}日
      if (string.IsNullOrEmpty(p_ReturnFormat)) p_ReturnFormat = "{0}岁{1}月{2}日";

      //定义:年、月、日
      int year, month, day;

      //计算:天
      day = p_SecondDateTime.Day - p_FirstDateTime.Day;
      if (day < 0)
      {
        day += System.DateTime.DaysInMonth(p_FirstDateTime.Year, p_FirstDateTime.Month);
        p_FirstDateTime = p_FirstDateTime.AddMonths(1);
      }
      //计算:月
      month = p_SecondDateTime.Month - p_FirstDateTime.Month;
      if (month < 0)
      {
        month += 12;
        p_FirstDateTime = p_FirstDateTime.AddYears(1);
      }
      //计算:年
      year = p_SecondDateTime.Year - p_FirstDateTime.Year;

      //返回格式化后的结果
      return string.Format(p_ReturnFormat, year, month, day);
    }

  • 相关阅读:
    JAVA 网格布局管理器
    JAVA 流式布局管理器
    JAVA 边界布局管理器
    JAVA 图形界面 JFrame容器
    MySQL联合查询语法内联、左联、右联、全联
    ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
    ASP.NET MVC URL重写与优化(初级篇)-使用Global路由表定制URL
    MVC过滤器详解
    Dapper的基本使用
    JQuery fullcalender文档
  • 原文地址:https://www.cnblogs.com/RoyalBlue/p/11225422.html
Copyright © 2011-2022 走看看