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
|
| Author: Ajesh Babu 04 Sep 2004 | Member Level: Silver Points : 0 |
Hi The portion about time differece is very good and it is simple. Thanks
|
| Author: Hristo Yankov 17 Aug 2005 | Member 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 2006 | Member 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: Meetu Choudhary 25 Sep 2008 | Member Level: Gold 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~~
|