简体   繁体   English

如何在C#中减去两个Datetime值?

[英]How to subtract two Datetime values in C#?

I want to subtract two Datetime values. 我想减去两个Datetime值。 I collected them separately from the form and concatenate it in C#. 我从表格中分别收集了它们,并使用C#将其连接起来。 The result shows in Hour format in textbox like "23:00". 结果以小时格式显示在“ 23:00”之类的文本框中。

Here is what I tried: 这是我尝试过的:

string start = StartDateTxtBox.Text+" "+StartHourDrop.Text+":"+StartMinuteDrop.Text+":00";
string End = EndDateTxtBox.Text + " " + EndHourDrop.Text + ":" + EndMinuteDrop.Text + ":00";

DateTime allstart = DateTime.Parse(start);
DateTime allEnd = DateTime.Parse(End);

Int32 MinuteDiff = Convert.ToInt32(allEnd.Subtract(allstart).TotalMinutes);
Int32 Hour = MinuteDiff / 60 / 24;
DateTime conversiontotime = System.Convert.ToDateTime(Hour);
LastID.Text = conversiontotime.ToString();

But it doesn`t work. 但这行不通。

Is there another (easy) way to do this, or can anybody tell me what the problem in my code is? 还有另一种(简便)的方法可以执行此操作,还是有人可以告诉我代码中的问题是什么?

    DateTime allstart = DateTime.Parse("03/04/2015 09:15");
    DateTime allEnd = DateTime.Parse("03/04/2015 09:37");
    TimeSpan tsdiff = allEnd - allstart;
    LastID.Text = tsdiff.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM