Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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# 时间跨度没有出现_C#_Winforms_Timespan - Fatal编程技术网

C# 时间跨度没有出现

C# 时间跨度没有出现,c#,winforms,timespan,C#,Winforms,Timespan,我似乎无法让我的代码计算两个datetime字段之间的分钟数: private void button4_Click(object sender, EventArgs e) { string startTime = "5/1/2008 1:00:00 PM"; string endTime = "5/1/2008 3:00:00 PM"; DateTime startTimeParse = DateTime.Parse(startTime, Cultu

我似乎无法让我的代码计算两个datetime字段之间的分钟数:

private void button4_Click(object sender, EventArgs e)
{
    string startTime = "5/1/2008 1:00:00 PM";
    string endTime = "5/1/2008 3:00:00 PM";

    DateTime startTimeParse = 
        DateTime.Parse(startTime, CultureInfo.InvariantCulture);
    DateTime endTimeParse = 
        DateTime.Parse(endTime, CultureInfo.InvariantCulture);

    MessageBox.Show(startTime);
    MessageBox.Show(endTime);

    TimeSpan result = endTimeParse - startTimeParse;
    int hours = result.Hours;
    int minutes = result.Minutes;
}
在调试中,结果仅为
00:00:00


如果您能告诉我如何使用MessageBox.show the different in mins?

根据您上次的评论,请尝试:

MessageBox.Show(result.TotalMinutes.ToString())

两个
DateTime
值之间的数学运算将生成一个
TimeSpan
值。所以你可以这样做:

(endTimeParse - startTimeParse).TotalMinutes;

“结果”是什么意思?在上面的例子中,
hours
minutes
的值是多少?我没有发现任何错误<代码>小时将为2,
分钟
将为
0
。结果应为22.5分钟,在提供的开始和结束时间内,应将
小时
设置为2,
分钟
设置为0。结果应该是
02:00:00
@PriceCheaperton
result
是一个
TimeSpan
。你认为应该是22.5分钟,我同意。分钟数将为0,因为它是两小时零分钟。总分钟数不做任何算术运算。