c# 计算两个日期之间相差的小时数 

解决方案 »

  1.   

    TimeSpan类有一个TotalHours属性。
    先将两个字符型日期转换为DateTime类型,然后计算TimeSpan,再调用TotalHours属性。
      

  2.   

                TimeSpan ts = dt1 - dt2;;
                ts.Hours就是你要的
      

  3.   


     DateTime dt1 = DateTime.Now;
                    DateTime dt2 = DateTime.Now.AddHours(-3).AddDays(-1);
                    TimeSpan ts = dt1.Subtract(dt2);
                    double hours = ts.TotalHours;//27.0
      

  4.   

     DateTime a = DateTime.Now;
     DateTime b = a.AddHours(5); int c =(b - a).Hours;DateTime有操作符运算        // Summary:
            //     Subtracts a specified date and time from another specified date and time
            //     and returns a time interval.
            //
            // Parameters:
            //   d1:
            //     A System.DateTime (the minuend).
            //
            //   d2:
            //     A System.DateTime (the subtrahend).
            //
            // Returns:
            //     A System.TimeSpan that is the time interval between d1 and d2; that is, d1
            //     minus d2.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static TimeSpan operator -(DateTime d1, DateTime d2);
            //
            // Summary:
            //     Subtracts a specified time interval from a specified date and time and returns
            //     a new date and time.
            //
            // Parameters:
            //   d:
            //     A System.DateTime.
            //
            //   t:
            //     A System.TimeSpan.
            //
            // Returns:
            //     A System.DateTime whose value is the value of d minus the value of t.
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     The resulting System.DateTime is less than System.DateTime.MinValue or greater
            //     than System.DateTime.MaxValue.
            public static DateTime operator -(DateTime d, TimeSpan t);
            //
            // Summary:
            //     Determines whether two specified instances of System.DateTime are not equal.
            //
            // Parameters:
            //   d1:
            //     A System.DateTime.
            //
            //   d2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if d1 and d2 do not represent the same date and time; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator !=(DateTime d1, DateTime d2);
            //
            // Summary:
            //     Adds a specified time interval to a specified date and time, yielding a new
            //     date and time.
            //
            // Parameters:
            //   d:
            //     A System.DateTime.
            //
            //   t:
            //     A System.TimeSpan.
            //
            // Returns:
            //     A System.DateTime that is the sum of the values of d and t.
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     The resulting System.DateTime is less than System.DateTime.MinValue or greater
            //     than System.DateTime.MaxValue.
            public static DateTime operator +(DateTime d, TimeSpan t);
            //
            // Summary:
            //     Determines whether one specified System.DateTime is less than another specified
            //     System.DateTime.
            //
            // Parameters:
            //   t1:
            //     A System.DateTime.
            //
            //   t2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if t1 is less than t2; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator <(DateTime t1, DateTime t2);
            //
            // Summary:
            //     Determines whether one specified System.DateTime is less than or equal to
            //     another specified System.DateTime.
            //
            // Parameters:
            //   t1:
            //     A System.DateTime.
            //
            //   t2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if t1 is less than or equal to t2; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator <=(DateTime t1, DateTime t2);
            //
            // Summary:
            //     Determines whether two specified instances of System.DateTime are equal.
            //
            // Parameters:
            //   d1:
            //     A System.DateTime.
            //
            //   d2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if d1 and d2 represent the same date and time; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator ==(DateTime d1, DateTime d2);
            //
            // Summary:
            //     Determines whether one specified System.DateTime is greater than another
            //     specified System.DateTime.
            //
            // Parameters:
            //   t1:
            //     A System.DateTime.
            //
            //   t2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if t1 is greater than t2; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator >(DateTime t1, DateTime t2);
            //
            // Summary:
            //     Determines whether one specified System.DateTime is greater than or equal
            //     to another specified System.DateTime.
            //
            // Parameters:
            //   t1:
            //     A System.DateTime.
            //
            //   t2:
            //     A System.DateTime.
            //
            // Returns:
            //     true if t1 is greater than or equal to t2; otherwise, false.
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            public static bool operator >=(DateTime t1, DateTime t2);
      

  5.   

    我也是TimeSpan 来解决 时间之间的时间差的。
      

  6.   


    Timespan = time1-time2
      

  7.   

    http://blog.csdn.net/xianfajushi/article/details/7532771
      

  8.   


    用timespan函数 变量 得到 两个时间的时差然后格式化 d 就得到了  或者直接取day