• 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);
    }

  • 相关阅读:
    yii 验证码功能的实现
    关于php优化 你必须知道的一些事情
    php实现两分法查找
    Python封装的访问MySQL数据库的类及DEMO
    新学习的Python的代码(while循环)
    基于位运算符的IP和数值转换
    JS数组操作常用方法
    JS输出日历
    PHP程序输出日历
    PHP中计算时间差(上周,上月,去年,昨天等)
  • 原文地址:https://www.cnblogs.com/RoyalBlue/p/11225422.html
Copyright © 2020-2023  润新知