ali raza

C#, .Net ,Asp.net and my thoughts

How do I find difference between two dates? In C#

DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);
// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12
);
// Time span
TimeSpan diffDate = endDate.
Subtract ( startDate );
// Spit it out
Response.Write( “Time Difference: <br/> “
);
Response.Write
(diffDate.Days.ToString() + ” Days <br/>”
);
Response.Write
(diffDate.Hours.ToString() + ” Hours <br/>”
);
Response.Write
(diffDate.Minutes.ToString() + ” Minutes <br/>”
);
Response.Write
(diffDate.Seconds.ToString() + ” Seconds <br/> “
);
Response.Write
(diffDate.Milliseconds.ToString() + ” Milliseconds <br/>”);

3 responses to “How do I find difference between two dates? In C#

  1. Linus January 23, 2008 at 10:09 am

    Great example!

    Used this in my application and it works perfectly 🙂

    /Cheers Linus

  2. Magnus October 24, 2008 at 7:45 am

    Thank you! 🙂

  3. Stefan December 23, 2008 at 3:25 pm

    Thanks! This was exactly what I was looking for.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: