bjh Hans
bjh Hans

Reputation: 989

how to minus two time period

I am developing a c#.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.

Upvotes: 0

Views: 992

Answers (3)

Kurru
Kurru

Reputation: 14331

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.

Upvotes: 1

Tarydon
Tarydon

Reputation: 5183

TimeSpan can be used to measure the differences between 2 DateTimes:

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

Upvotes: 8

Kobi
Kobi

Reputation: 138067

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

Upvotes: 4

Related Questions