C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to find difference between two Dates in C# or VB.NET ?


Posted Date: 16 Jun 2004    Resource Type: Articles    Category: .NET Framework
Author: Mary MathewMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction

It was an easy job with classic VB to find difference between 2 dates or times. Even though it is not very difficult, it is a tricky and confusing thing in C# and VB.NET.

See the following sample code, which demonstrates how to calculate the difference between two DateTime objects using C# syntax.

DateTime startTime = DateTime.Now;

DateTime endTime = DateTime.Now.AddSeconds( 75 );

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

In the above sample, we are creating two datetime objects, one with current time and another one with 75 seconds added to the current time. Then we will call the method .Subtract() on the second DateTime object. This will return a TimeSpan object.
Once we get the TimeSpan object, we can use the properties of TimeSpan to get the actual Hours, Minutes and Seconds.
Output:

Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0

In the above example, if you think the .Seconds property will return 75 (the difference between two times we are comparing is 75 seconds), that's a wrong assumption!! The Microsoft team has done it bit confusing. The .Seconds property will return 15 and the .Minutes property will return 1. So, you have to combine all these properties together to find the actual time difference. And unfortunately, you cannot get the Months and Years from the TimeSpan. The maximum you can get is the number of Days!

You can use the same above approach in VB.NET also, with the familiar VB syntax. But most of the VB guys prefer to use the old DateDiff function, which is very easy to use and also allows you to get the number of Months, years etc. It would have been very easier if the .NET team had provided a method equivalent to the classic VB DateDiff function.

There is an interesting discussion here, where Microsoft people are involved : http://blogs.msdn.com/vbfaq/archive/2004/05/30/144571.aspx




Responses

Author: Ajesh Babu    04 Sep 2004Member Level: Silver   Points : 0
Hi
The portion about time differece is very good and it is simple.
Thanks


Author: Hristo Yankov    17 Aug 2005Member Level: Bronze   Points : 0
"In the above example, if you think the .Seconds property will return 75 (the difference between two times we are comparing is 75 seconds), that's a wrong assumption!! The Microsoft team has done it bit confusing. The .Seconds property will return 15 and the .Minutes property will return 1. So, you have to combine all these properties together to find the actual time difference."

Have you heard of the TotalMinutes propertie of the TimeSpan class?!


Author: Chetna    21 Sep 2006Member Level: Bronze   Points : 0
Thanks for code for time difference .for such a simple thing i was doing lot of coding..Net made it quite simple.
Thanks
again


Author: Miss Meetu Choudhary    25 Sep 2008Member Level: Diamond   Points : 1
Hi The Topic Is Very Well Written and Good TO Understand.....
Really Useful Information........



--
I Would Love To Change The World But They Wont Give Me The Source Code

Spread D Smile n Never Say Bye

May Lord Shiva Bless All of US
Thanks and Regards
Meetu Choudhary
~~MEET~~


Author: arun    30 Nov 2008Member Level: Bronze   Points : 0
thanks for coding

Thanks
arun


Author: Fool To Code    02 Dec 2008Member Level: Gold   Points : 1
very nice code.

i think we can use DateDiff() function also to get difference between two dates.

but your code is great


Author: sangeetha    06 Mar 2009Member Level: Gold   Points : 1
DateTime sdate = Convert.ToDateTime(txtFromDate.Value);
DateTime edate = Convert.ToDateTime(txtToDate.Value);
TimeSpan ts = edate - sdate;
int days = ts.Days;


Author: Karl    17 May 2009Member Level: Bronze   Points : 0
Sangeetha -

Very nice! Works brilliantly.

Karl66


Author: Deepika Haridas    17 May 2009Member Level: Diamond   Points : 1
Hi,

Very good article..
Useful for all..

Thanks for sharing..

Regards,
Deepika


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Panel  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Parsing of XML data [DOM traversal]
Previous Resource: How to find free disk space and total space of a disk drive
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use