How to use datediff in c#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: How to use datediff in c#

  1. #1
    Join Date
    Jan 2005

    How to use datediff in c#

    Hi everyone,

    is there a function that calculates the exact amount of Years, Months, and
    Days between two days?

    TimeDiff is not good enough, since it calculates, for example:

    Date 1: Dec. 31st 2003
    Date 2: Jan 1st 2004

    The real difference is 0 years, 0 months, 1 day. DateDiff returns 1 year, 1
    month, 1 day.

    Is there a funcion, or rutine, or does anyone know a good way to achieve the
    desired result?

  2. #2
    Join Date
    Mar 2006
    Graz, Austria

    Re: How to use datediff in c#

    here is an example:

    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 );

    Hope it helped
    I would love to change the world, but they won't give me the source code

  3. #3
    Join Date
    Jul 2001
    Sunny South Africa

    Re: How to use datediff in c#

    @tis707: The DateDiff function is not available in C#, have a look at this workaround

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)