Calculating date differences in C# (DateDiff for c#??)

  • Thread starter Thread starter Paul Aspinall
  • Start date Start date
P

Paul Aspinall

Hi
I want to calculate the difference between 2 dates in C#.

I know there is a function in VB, called DateDiff, but I don't want to ref
the VB library, and want to try to do it natively in C#.

Is there a similar function available in the C# or standard .NET libraries?


Thanks
 
Look up the Subtraction operator of the DateTime and TimeSpan structures.
You can get the difference between combinations of DateTimes, TimeSpans
returned as either DateTimes or TimeSpans

HTH

DalePres
MCAD, MCDBA, MCSE
 
Look at System.DateTime and System.TimeSpan.

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatetimeclasstopic.asp
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemtimespanclasstopic.asp

DateTime dtThen = new DateTime (2004, 1, 1);
DateTime dtNow = DateTime.Now;
TimeSpan ts = dtNow - dtThen;

DateTime also contains functions like AddHours & AddMinutes. I don't think
there is any direct equivalent to DateDiff, but you can certainly accomplish
the same thing in other ways using DateTime and TimeSpan.

- Jon
 
Back
Top