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

Thread: How to use datediff in c#

  1. #1
    Join Date
    Jan 2005
    Posts
    224

    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
    Location
    Graz, Austria
    Posts
    273

    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
    Daniela
    ******
    I would love to change the world, but they won't give me the source code

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,092

    Re: How to use datediff in c#

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

    http://www.dotnetspider.com/kb/Article1552.aspx

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center