Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取Asp.net中两次之间的小时数_C#_Asp.net_Timespan_Tryparse - Fatal编程技术网

C# 获取Asp.net中两次之间的小时数

C# 获取Asp.net中两次之间的小时数,c#,asp.net,timespan,tryparse,C#,Asp.net,Timespan,Tryparse,我在.aspx中有一个3文本框。 1.txtStartTime 2.txtEndTime 3.txtNumberOfHours 我想在txtNumberOfHours 这是我的密码: TestNumOfHour.aspx <asp:TextBox ID="txtStartTime" runat="server" ontextchanged="txtStartTime_TextChanged" AutoPostBack="true"></asp:TextBox> <a

我在.aspx中有一个3文本框。
1.txtStartTime
2.txtEndTime
3.txtNumberOfHours

我想在txtNumberOfHours

这是我的密码:

TestNumOfHour.aspx

<asp:TextBox ID="txtStartTime" runat="server" ontextchanged="txtStartTime_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtEndTime" runat="server" ontextchanged="txtEndTime_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtNumberOfHours" runat="server"></asp:TextBox>
我只想小时排除分和秒,但不知怎的,我得到了这个结果

txtNumberOfHours:hr:mins:seconds中的结果

试试这个:

(DateTime.Parse(t2) - DateTime.Parse(t1)).TotalHours

此语句返回小时数。

输入的是“一天中的抽象时间”还是特定日期的(日期)时间?如果是后者,你需要考虑DST吗?查看<代码> TimeSpA.小时>代码>进入<代码> TimeStudio。toString(String)可能的副本可能也会考虑使这一切都不那么“严格类型化”。没有理由让
CalculateMediff
方法解析字符串。它的输入应该是
DateTime
s。如果startTime是
00:00:00
,endTime也是
?如果开始时间是
00:00:00
,结束时间是
23:59:59
?如果startTime是
23:00:00
endTime
01:00:00
,那该怎么办?这正是我想要的。谢谢
(DateTime.Parse(t2) - DateTime.Parse(t1)).TotalHours
txtNumberOfHours.Text = (t2 - t1).TotalHours.ToString();