Solved

Which is the best way to find Date difference between two dates in C#?

Posted on 2010-09-08
4
339 Views
Last Modified: 2012-05-10
Please submit a code snippet which is most efficient one.. I know about TimeSpan Class Method is there any other method thats more effective?
0
Comment
Question by:starlite551
4 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33633883
Not that I know off. I thought it better to use what is available in dotnet and for c# that seems to be it.

A little discussion on that here including an alternative using vb namespaces http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33633957
dim date1 as datetime = ...
dim date2 as datetime = ...

dim datediff = date2.subtract(date1)

This gives you a TimeSpan object with all the information.  What makes you think this is not efficient?
0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 125 total points
ID: 33634192
actually DateTime.Subtract method should be fine as it is just doing simple subtraction, so this method will be too fast and good.
 
 

public TimeSpan Subtract(DateTime value)

{

    return new TimeSpan(this.InternalTicks - value.InternalTicks);

}



public DateTime Subtract(TimeSpan value)

{

    long internalTicks = this.InternalTicks;

    long num2 = value._ticks;

    if ((internalTicks < num2) || ((internalTicks - 0x2bca2875f4373fffL) > num2))

    {

        throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_DateArithmetic"));

    }

    return new DateTime(((ulong) (internalTicks - num2)) | this.InternalKind);

}

Open in new window

0
 
LVL 14

Accepted Solution

by:
existenz2 earned 125 total points
ID: 33661625
Using substract is the most efficient. Using ticks is a bit slower (see code example):
DateTime now = DateTime.Now;

            DateTime last = DateTime.Now.AddHours(-4);



            Stopwatch watch = new Stopwatch();

            watch.Start();

            for(int i = 0; i < 1000000; i++)

            {

                TimeSpan timeSpan = now.Subtract(last);

            }

            watch.Stop();

            Console.WriteLine(watch.ElapsedMilliseconds);



            Stopwatch watch2 = new Stopwatch();

            watch2.Start();

            for (int i = 0; i < 1000000; i++)

            {

                long ticks = now.Ticks - last.Ticks;

            }

            watch2.Stop();

            Console.WriteLine(watch2.ElapsedMilliseconds);



            Console.Read();

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 157
Entity Framework Error: 'The user instance login flag is not supported' 12 32
Adjust the position 3 45
Server Error 11 47
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now