简体   繁体   中英

how to minus two time period

I am developing ac#.net app in which I need to subtract two time periods. 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:

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

Check the TimeSpan struct.
Also, for DateTime, you have handy procedures such as 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

Subtracting one DateTime from another returns a Timespan object. which basically tells you how many days/hours/mins/secs/milliseconds/ticks that occured between the 2 DateTimes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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