locked
Comparing two dates excluding seconds and milliseconds RRS feed

  • Question

  • I have two datetimes

    I am comparing these like

    if(d1<d2) 

    do something.....

    This is comparing seconds also. I want to comapre these days upto minutes only excluding seconds and milliseconds. Please help.


    Gaurav
    Thursday, April 29, 2010 11:41 AM

Answers

  • if(d1.Date.AddHours(d1.Hour).AddMinutes(d1.Minute)<d2.Date.AddHours(d2.Hour).AddMinutes(d2.Minute))

    or

    if(d1.ToString("yyyyMMddhhmm")<d2.ToString("yyyyMMddhhmm"))

    or

    if(d1<d2 || d1.Date==d2.Date && d1.Hour==d2.Hour && d1.Minute==d2.Minute)

    Thursday, April 29, 2010 1:25 PM
  • try this it will help you-

               d1 = d1.AddMilliseconds(-d1.Millisecond);
                d1 = d1.AddSeconds(-d1.Second);
                d2 = d2.AddMilliseconds(-d2.Millisecond);
                d2 = d2.AddSeconds(-d2.Second);

    then

    if(d1<d2) 

    do something.....

     

     

    regards

    jayant

    Thursday, April 29, 2010 1:27 PM
  • I think you're probably going about this wrong. All you're doing is introducing roundoff error, and I'm having a hard time understanding the purpose of this requirement.

    If you want to test whether two datetimes are within a minute of each other, then that's different:
      if ((d2 - d1).Duration() < TimeSpan.FromMinutes(1))

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Thursday, April 29, 2010 1:43 PM

All replies

  • if(d1.Date.AddHours(d1.Hour).AddMinutes(d1.Minute)<d2.Date.AddHours(d2.Hour).AddMinutes(d2.Minute))

    or

    if(d1.ToString("yyyyMMddhhmm")<d2.ToString("yyyyMMddhhmm"))

    or

    if(d1<d2 || d1.Date==d2.Date && d1.Hour==d2.Hour && d1.Minute==d2.Minute)

    Thursday, April 29, 2010 1:25 PM
  • try this it will help you-

               d1 = d1.AddMilliseconds(-d1.Millisecond);
                d1 = d1.AddSeconds(-d1.Second);
                d2 = d2.AddMilliseconds(-d2.Millisecond);
                d2 = d2.AddSeconds(-d2.Second);

    then

    if(d1<d2) 

    do something.....

     

     

    regards

    jayant

    Thursday, April 29, 2010 1:27 PM
  • I think you're probably going about this wrong. All you're doing is introducing roundoff error, and I'm having a hard time understanding the purpose of this requirement.

    If you want to test whether two datetimes are within a minute of each other, then that's different:
      if ((d2 - d1).Duration() < TimeSpan.FromMinutes(1))

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Thursday, April 29, 2010 1:43 PM