谢谢

解决方案 »

  1.   

    DateTime dt1 = DateTime.Now;
    DateTime dt2 = new DateTime(2005, 3, 1);
    int year = dt1.Year - dt2.Year; //相差的年份
    int month = dt1.Year * 12 + dt1.Month - dt2.Year * 12 - dt2.Month; //相关的月份
      

  2.   

    除了TimeSpan好像没有别的可用了吧.
    如果不考虑闰年,可以用
    TotalDays/365来得到年差的表示
    TotalDays/30来得到月差的表示因为这些值和当时的年份月份有关,所以不能得到准确的年差月差(我的理解)
      

  3.   

    tryDateTime dt1 = new DateTime(2005, 12, 1);
    DateTime dt2 = System.DateTime.Now;
    int Year = dt2.Year - dt1.Year;
    int Month = (dt2.Year - dt1.Year) * 12 + (dt2.Month - dt1.Month);
      

  4.   

    好的谢谢
    也就是没有像SQL或VB的
    DateDiff是不是
      

  5.   

    DateTime dt = DateTime.Now;
    DateTime temp = new DateTime(2004,10,5,1,1,1);
    string year = (dt.Year - temp.Year).ToString();
    string month = (dt.Month - temp.Month).ToString();
      

  6.   

    如果在SQL直接datediff,比較方便。
      

  7.   

    办法很多种
    如:计算月差还可以    Convert.ToInt32(dt2.ToString("yyyyMM"));
            -
            Convert.ToInt32(dt1.ToString("yyyyMM"));
      

  8.   

    TimeSpan 应该可以得到年差与月差
      

  9.   

    时间里面的年,月,日都是int类型,有必要转换成string吗?