简体   繁体   English

如何减去两个时间段

[英]how to minus two time period

I am developing ac#.net app in which I need to subtract two time periods. 我正在开发ac#.net应用程序,其中我需要减去两个时间段。 I have taken two date objects and subtracted them but it doesn't work. 我已经取了两个日期对象并减去了它们,但它不起作用。

TimeSpan can be used to measure the differences between 2 DateTimes: TimeSpan可用于测量2个DateTime之间的差异:

DateTime dt1 = ...
DateTime dt2 = ...
TimeSpan diff = dt2 - dt1;

Check the TimeSpan struct. 检查TimeSpan结构。
Also, for DateTime, you have handy procedures such as AddDays : 另外,对于DateTime,您可以使用便捷的程序,例如AddDays

DateTime later = mydate.AddDays(1.0);

Similarlly, there are AddHours , AddMonths and even AddMilliseconds : http://msdn.microsoft.com/en-us/library/system.datetime_members.aspx 类似地,还有AddHoursAddMonths甚至AddMillisecondshttp : //msdn.microsoft.com/en-us/library/system.datetime_members.aspx

Subtracting one DateTime from another returns a Timespan object. 从另一个减去一个DateTime将返回一个Timespan对象。 which basically tells you how many days/hours/mins/secs/milliseconds/ticks that occured between the 2 DateTimes. 它基本上告诉您在两个DateTime之间发生了多少天/小时/分钟/秒/毫秒/滴答声。

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

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