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

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
 
D

DalePres

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
 
J

Jon Sagara

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top