Computing age

/// <the Summary>
/// calculate the age of the string (years of age)
/// default return: xx xx xx-year-old Japanese
/// </ the Summary>
/// <param name = "p_FirstDateTime"> 1st date parameters </ param>
/// <param name = "p_SecondDateTime"> second date parameter </ param>
/// <param name = "p_Format"> returns the string format, default: {{0} years 1} {2} month day </ param>
Private static String CalculateAgeString (the DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, String p_ReturnFormat)
{
  // determines whether the time period is positive. If it is negative, two points in time swap positions.
  IF (System.DateTime.Compare (p_FirstDateTime, p_SecondDateTime)> 0)
  {
    System.DateTime stmpDateTime = p_FirstDateTime;
    p_FirstDateTime = p_SecondDateTime;
    p_SecondDateTime = stmpDateTime;
  }

  // returns the format string is determined. If it is empty, a default value: {1} {0} years {2} January day
  if (string.IsNullOrEmpty (p_ReturnFormat)) p_ReturnFormat = "{0} years {2} {1} January Day";

  // define: year, month, day,
  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;

  // returns the formatted result
  return string.Format (p_ReturnFormat, year, month The, Day);
}

Guess you like

Origin www.cnblogs.com/RoyalBlue/p/11225422.html