I have seen some answers for Java, Javascript, and PHP, but nothing for C#. So, how one might calculate the number of days between two dates in C#?
Thanks.
Assuming StartDate
and EndDate
are of type DateTime
:
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now.AddDays(-5);
(date1 - date2).TotalDays
Advanced code:
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);
TimeSpan t = d1 - d2;
double Difference = t.TotalDays;
I hope it helped you!
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!