用户在页面输入一个设备的开始运行时间(2008-5-1 12:00:00)结束停止(2008-5-10 12:00:00)时间,怎样才能知道这个设备运行了多少个小时呢?
用TimeSpan方法可以算出来,但是不知道怎么使用,还请大哥们将详细方法写出,已便以后的使用???小弟多谢了。

解决方案 »

  1.   

     TimeSpan dt = date2- date1;
     double totalhours = dt.TotalHours;
     (data2为停止时间,data1为开始运行时间)
      

  2.   

    这个是方法public int getHH(string str1,string str2) {
            DateTime dt1 = Convert.ToDateTime(str1);
            DateTime dt2 = Convert.ToDateTime(str2);
            TimeSpan ts = dt2.Subtract(dt1);
            int hh = ts.Hours;
            return hh;
        }你可以取了用户输入的值,比如string strT1 = this.TextBox1.Text;
    string strT2 = this.TextBox2.Text;
    //这样调用
    int i=getHH(strT1,strT2);