一个日期减去另一个日期,如何得到相减的月份?
比如:

解决方案 »

  1.   

    TimeSpan span = DateTime.Parse("2006-10-1") -  DateTime.Parse("2006-9-1") ;
    int nMonth = span.Month;
      

  2.   

    DateTime dt1 = Convert.ToDateTime("2006-9-1");
    DateTime dt2 = Convert.ToDateTime("2006-10-1");
    int i = dt1.Month - dt2.Month;就可以了如果就要正数,那么判断,对负取正
      

  3.   

    int nMonth = span.Month;
    好象没有Month 这个属性吧?
      

  4.   

    不好意思,确实没有int nMonth = span.Month这个属性,我是随手写的.
      

  5.   

    DateTime dt1=DateTime.Parse("2006-9-1");
    DateTime dt2=DateTime.Parse("2006-10-1");
    int m=dt1.Month-dt2.Month;
    //或 Math.Abs(dt1.Month-dt2.Month);
      

  6.   

    jcyluck(C# + SQL 2005 QQ群:26096739) 说的很清楚 :用DateTime.Month进行运算
    t1.Month - t2.Month
      

  7.   

    DateTime dt1 = DateTime.Parse("2006-01-01");
    DateTime dt2 = DateTime.Parse("2005-01-02"); if(dt1.Day < dt2.Day)
    {
    dt1 = dt1.AddMonths(-1);
    } int iM = (dt1.Year*12 +dt1.Month) - (dt2.Year*12 + dt2.Month);
    MessageBox.Show(iM.ToString());