Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get the difference between two datesك

Example:
2011/10/10
2011/10/12
-----------
day:2

Note: 29 days and 31 days to calculate
Posted

You use a TimeSpan to retrieve the difference. Consider this example:
C#
DateTime startDate = DateTime.Now.AddDays(-25);
DateTime endDate = DateTime.Now;
TimeSpan difference = endDate.Subtract(startDate);
Console.WriteLine(difference.Days);
 
Share this answer
 
Use DateTime.Subtract(DateTime) function.

C#
DateTime christmas = new DateTime(2011, 10, 10);
DateTime newYears = new DateTime(2011, 10, 12);
TimeSpan span = newYears.Subtract(christmas);

console.Writeline(span);


Source:http://www.dotnetperls.com/datetime-subtract[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900