您的位置:首页 > 编程语言 > C#

C# 获取两个时间相差年月

2012-05-15 10:56 661 查看
public static string GetDateYAndM(string beginDate, string endDate)
{
  var bdate= Convert.ToDateTime(beginDate);
  var edate= Convert.ToDateTime(endDate);

  int years = -1;
  int months = -1;

  if (bdate> edate)
    return null;

  while (bdate<= edate)
  {
    bdate= bdate.AddYears(1);
    years++;
  }
  if (years < 2)
  {
    bdate= Convert.ToDateTime(beginDate);
    while (bdate<= edate)
    {
      bdate= bdate.AddMonths(1);
      months++;
    }
}
  return years.toString()+"-"+months.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: