How can I calculate difference between two dates in C#?

0
=
0
+
0
No specific Bitcoin Bounty has been announced by author. Still, anyone could send Bitcoin Tips to those who provide a good answer.
0

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.

1 Answer

1
=
0
=
$0
Internet users could send Bitcoin Tips to you if they like your answer!

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!

SEND BITCOIN TIPS
0

Too many commands? Learning new syntax?

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!

Post Answer