C# - 计算8:00到2:00和8:00到12:00之间的总小时数

时间:2017-02-02 06:16:37

标签: c#

这是我的代码

DateTime TimeIn = "8:00 AM",
         TimeOut="2:00 AM";
double Total;

private void compute()
{
    Total = (TimeOut - TimeIn).TotalHours;
}
{p> 8:00am to 2:00am会产生18 hours。但我的结果是-7 另一个问题是当我输入24:00时超时C#无法将其识别为时间。

TimeOout小于12:00am时,它可以正常工作。像11:59pm一样倒退。 (eg.: 11:30PM - 8:00AM)它正确计算。

请帮助。

3 个答案:

答案 0 :(得分:0)

可能会对您有所帮助:

string TimeIn= "8:00 AM";
string TimeOut= "2:00 AM";

TimeSpan duration = DateTime.Parse(TimeOut).Subtract(DateTime.Parse(TimeIn));

答案 1 :(得分:0)

否定结果时加+24小时。

答案 2 :(得分:0)

您声明日期时间值的方式是错误的。正确的方法是将字符串转换为日期时间格式

doughnutRadius : Number [-]
This attribute lets you explicitly set the inner radius of the chart. FusionCharts XT automatically calculates the best fit radius for the chart. This attribute is useful if you want to enforce one of your own values.

Range: In Pixels

如果您需要18小时的时间跨度,那么您在为变量分配数据时也必须传递日期值