如何求2个DATETIME之间相差几小时?(考虑跨年)
在线等 谢谢

解决方案 »

  1.   

    ).TotalHours 是哪个包里的方法?
      

  2.   

    DateTime里的静态方法吗?找不到?
      

  3.   

    取当前时 
    int t1=DATETIME1.Hour; 
    int t2=DATETIME2.Hour;
    然后相减
      

  4.   

    DateTime - DateTime = TimeSpan
    TotalHours就是TimeSpan的。
      

  5.   

    TimeSpan span = dt1 - dt2;
    double cnt = span.TotalHours;
      

  6.   

    不好意思。。漏给 LVONY分数了 第一次结分 。。没经验 多见量
      

  7.   

    using System;class TimeSpanPropertiesDemo
    {
        const string headerFmt = "\n{0,-45}";
        const string dataFmt = "{0,-12}{1,8}       {2,-18}{3,21}" ;    // Display the properties of the TimeSpan parameter.
        static void ShowTimeSpanProperties( TimeSpan interval )
        {
            Console.WriteLine( "{0,21}", interval );
            Console.WriteLine( dataFmt, "Days", interval.Days, 
                "TotalDays", interval.TotalDays );
            Console.WriteLine( dataFmt, "Hours", interval.Hours, 
                "TotalHours", interval.TotalHours );
            Console.WriteLine( dataFmt, "Minutes", interval.Minutes, 
                "TotalMinutes", interval.TotalMinutes );
            Console.WriteLine( dataFmt, "Seconds", interval.Seconds, 
                "TotalSeconds", interval.TotalSeconds );
            Console.WriteLine( dataFmt, "Milliseconds", 
                interval.Milliseconds, "TotalMilliseconds", 
                interval.TotalMilliseconds );
            Console.WriteLine( dataFmt, null, null, 
                "Ticks", interval.Ticks );
        }     static void Main( )
        {
            Console.WriteLine(
                "This example of the TimeSpan class properties " +
                "generates the \nfollowing output. It " +
                "creates several TimeSpan objects and \ndisplays " +
                "the values of the TimeSpan properties for each." );        // Create and display a TimeSpan value of 1 tick.
            Console.Write( headerFmt, "TimeSpan( 1 )" );
            ShowTimeSpanProperties( new TimeSpan( 1 ) );        // Create a TimeSpan value with a large number of ticks.
            Console.Write( headerFmt, "TimeSpan( 111222333444555 )" );
            ShowTimeSpanProperties( new TimeSpan( 111222333444555 ) );        // This TimeSpan has all fields specified.
            Console.Write( headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )" );
            ShowTimeSpanProperties( new TimeSpan( 10, 20, 30, 40, 50 ) );        // This TimeSpan has all fields overflowing.
            Console.Write( headerFmt, 
                "TimeSpan( 1111, 2222, 3333, 4444, 5555 )" );
            ShowTimeSpanProperties(
                new TimeSpan( 1111, 2222, 3333, 4444, 5555 ) );        // This TimeSpan is based on a number of days.
            Console.Write( headerFmt, "FromDays( 20.84745602 )" );
            ShowTimeSpanProperties( TimeSpan.FromDays( 20.84745602 ) );
        } 
    }