解决方案 »

  1.   

    timespan 可以计算返回天数、小时数等,但月份那个怎么计算呢?还有我只想计算日期,时分秒不管。我是用dateTimePicker日期控件选择日期,timespan ts=date1-date2 计算返回的天数不准确,还有时分秒。
      

  2.   

    人家返回的天数带时分秒那是精确,到你这就变成不准确了,你自己决定超过整数天是取整数还是整数天加1不就行了么。
    月份,你用TotalDays来/30算也行。就像要非常不精确的月份差,那么年份差*12-月份差就是了。
      

  3.   

    不要用timespan,你这个问题根本不是要把两个时间相减
    1.先把字符串转datetime类型,然后获取到datetime.year和datetime.month
    2.然后就可以循环了
      

  4.   


    /// <summary>
            /// 计算时间差
            /// </summary>
            /// <param name="t">时间1</param>
            /// <param name="t2">时间2</param>
            /// <returns>返回值:时间差(毫秒为单位)</returns>
            private static long TimeDiff(DateTime t, DateTime t2)
            {
                long lReturn = -1;
                System.TimeSpan NowValue = new TimeSpan(t.Ticks);
                System.TimeSpan TimeValue = new TimeSpan(t2.Ticks);
                System.TimeSpan DateDiff = TimeSpan.Zero;
                try
                {
                    //计算时间差
                    //DateDiff = TimeValue.Subtract(NowValue).Duration();
                    DateDiff = TimeValue.Subtract(NowValue);
                    int hours = DateDiff.Hours;
                    int minutes = DateDiff.Minutes;
                    int seconds = DateDiff.Seconds;
                    int milliseconds = DateDiff.Milliseconds;
                    string TimeDiff = hours.ToString() + ":"
                        + minutes.ToString() + ":"
                        + seconds.ToString() + "."
                        + milliseconds.ToString();
                   
                    //是否比现在的时间小
                    if (hours <= 0 && minutes <= 0 && seconds <= 0 && milliseconds <= 0)
                        lReturn = 0;
                    else
                    lReturn = hours * 3600 * 1000
                        + minutes * 60 * 1000
                        + seconds * 1000
                        + milliseconds;
                }
                catch (Exception )
                {
                    return -1;
                }
                return lReturn;
            }
      

  5.   


    List<string> GetFileNames(DateTime Date1, DateTime Date2)
    {
        List<string> FileNameList = new List<string>();
        int year1, year2;
        int month1, month2;    if (Date1 < Date2)
        {
            year1 = Date1.Year;
            year2 = Date2.Year;
            month1 = Date1.Month;
            month2 = Date2.Month;
        }
        else
        {
            year1 = Date2.Year;
            year2 = Date1.Year;
            month1 = Date2.Month;
            month2 = Date1.Month;
        }
        while (year1 != year2 && month1 != month2)
        {
            FileNameList.Add(year1.ToString() + month1.ToString("D2"));
            month1++;
            if (month1 > 12)
            {
                year1++;
                month1 = 1;
            }
        }    return FileNameList;
    }
      

  6.   


    DateTime startDate = DateTime.Parse("2014-1-1");
                    DateTime endDate = DateTime.Parse("2014-2-3");
                    TimeSpan ts = new TimeSpan();
                    int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;
                    ts = endDate - startDate;
                    Response.Write("相隔月份:" + totalMonth + ",相隔天数:" + ts.Days);