none
How do I calculate the number of days between two dates in C#?

Answers

  • Dear Josh,
    This can be easily accomplished using an object of Type "TimeSpan". For example: let's assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:

    DateTime d1=DateTime.MinValue;
    DateTime d2=DateTime.MaxValue;
    TimeSpan span=d2-d1;
    Console.WriteLine
             ( "There're {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );

    Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.

    Hope this could be helpful,
    Regards,
    Friday, April 08, 2005 1:09 AM

All replies


  • Here is one way to accomplish this...


     private int daysPast(DateTime oldDate)
     {
     
      long tickDiff = DateTime.Now.Ticks - oldDate.Ticks;
      tickDiff = tickDiff / 10000000; //have seconds now
      _age = (int)(tickDiff / 86400);
      return _age;//should be days
     }

    Friday, April 08, 2005 12:34 AM
  • Dear Josh,
    This can be easily accomplished using an object of Type "TimeSpan". For example: let's assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:

    DateTime d1=DateTime.MinValue;
    DateTime d2=DateTime.MaxValue;
    TimeSpan span=d2-d1;
    Console.WriteLine
             ( "There're {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );

    Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.

    Hope this could be helpful,
    Regards,
    Friday, April 08, 2005 1:09 AM
  • Yup, your right. That was fast!  I've actually gone ahead and marked your post as the correct answer.  To keep the FAQ clean I'll eventually delete my first reply that was posted as part of a forum demo I was recording. 

    There are also a couple of other obvious bugs in the code that I posted. :-)
    Friday, April 08, 2005 1:15 AM
  • Another way, using only DateTime:



    private int GetDaysBetweenDates(DateTime firstDate, DateTime secondDate)
    {
       return secondDate.Subtract(firstDate).Days;
    }

     
    PS: The code tags put all my code on one line, not me! Smile

    One bug in your original code is that the name _age does not exist.  The fix:

    int _age = (int)(tickDiff/86400);
     


    Another one is that you assume all days are 86400 seconds long. But there is one day that has 82800 seconds and another that has 90000 (for daylight savings).  This could introduce a bug.

    • Proposed as answer by JCMaia Monday, August 11, 2008 11:55 PM
    Friday, April 08, 2005 5:35 PM
  • Here's one way:
    DateTime now = DateTime.Now;
    DateTime then = new DateTime (1, 1, 2005);
    TimeSpan diff = now - then;
    int days = diff.Days;
    Monday, April 11, 2005 6:03 PM
  • This code was very helpful!  I've always reverted back to the Delphi TDateTime class to do my date calculations, but now that I know about TimeSpan I can do it all in C#.NET.  Thanks!
    Saturday, September 30, 2006 11:52 PM
  • Here's also a suggestion:

     

    Code Snippet

    private int DateDiffInDays(DateTime fromDate, DateTime toDate)

    {

    return toDate.Subtract(fromDate).Days;

    }

     

     

    • Proposed as answer by travich Friday, May 07, 2010 7:56 PM
    Friday, June 01, 2007 8:10 PM
  • I like Mohamed's solution -- it's the easiest and can be done in one line.
    Travich
    Friday, May 07, 2010 7:57 PM
  • (toDate - fromDate).Days

    only takes one line too.

    Sunday, May 09, 2010 11:42 PM