Calculate data time

  • Thread starter Thread starter Grey
  • Start date Start date
G

Grey

how can i get the difference and sum when I pass two dates.e.g.
2004/7/20 - 2004/7/3 = 17

2004/7/20 + 1 = 20047/21 or
2004/7/31 + 1 = 2004/8/1

Million thanks
 
Hi,

I suggest you read the documentation for System.DateTime and System.TimeSpan
structures:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDateTimeClassTopic.asp
http://msdn.microsoft.com/library/en-US/cpref/html/frlrfSystemTimeSpanClassTopic.asp

The Subtraction operator when applied on two DateTime structures results in
a TimeSpan instance that represents the difference. Then use the Days
property of this structure to get the number of whole days represented by
the instance:

[C#]
TimeSpan span1 = date1 - date2;
int days = span1.Days;

For the second part of your question - you should be using the method
AddDays on a DateTime instance:

DateTime date2 = date1.AddDays(1);

Hope this helps
Martin
 
Back
Top