Comparing dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Harvey

    #1

    Comparing dates

    Hi everyone,

    I need to be able to compare to dates to ensure that one is at least 1 day
    greater than the other.

    Im trying to do

    if(toDate !> fromDate){
    // handle
    }

    This works but the problem is its comparing the datetime down to the minute
    and second. I need to be accurate only to the date. Otherwise, a comparison
    for two datetimes that are within the same date, but of different times will
    give a false positive.

    Can anyone tell me how to ensure that one datetime is at least one day greater
    than the other?

    Many thanks

    Simon


  • Vadym Stetsyak

    #2
    Re: Comparing dates

    Hello, Simon!

    one way

    DateTime dt1 = DateTime.Now;
    DateTime dt2 = DateTime.Now;
    dt2 = dt2.AddDays(2);
    TimeSpan span = dt2 - dt1;
    if ( span.Days > 1 )
    else

    another one

    or you can use DateTime.DayOfW eek or Day, or DayOfYear dependin on the scope of dates being compared
    --
    Regards, Vadym Stetsyak
    www: http://vadmyst.blogspot.com

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Comparing dates

      Simon Harvey <nothanks@hotma il.com> wrote:[color=blue]
      > I need to be able to compare to dates to ensure that one is at least 1 day
      > greater than the other.
      >
      > Im trying to do
      >
      > if(toDate !> fromDate){
      > // handle
      > }
      >
      > This works but the problem is its comparing the datetime down to the minute
      > and second. I need to be accurate only to the date. Otherwise, a comparison
      > for two datetimes that are within the same date, but of different times will
      > give a false positive.
      >
      > Can anyone tell me how to ensure that one datetime is at least one day greater
      > than the other?[/color]

      if (toDate.Date > fromDate.Date)
      {
      ...
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Michael H

        #4
        Re: Comparing dates

        On Tue, 14 Feb 2006 08:16:33 -0800, Simon Harvey
        <nothanks@hotma il.com> wrote:

        -> Hi everyone,
        ->
        -> I need to be able to compare to dates to ensure that one is at
        least 1 day
        -> greater than the other.
        ->
        -> Im trying to do
        ->
        -> if(toDate !> fromDate){
        -> // handle
        -> }
        ->
        -> This works but the problem is its comparing the datetime down to
        the minute
        -> and second. I need to be accurate only to the date. Otherwise, a
        comparison
        -> for two datetimes that are within the same date, but of different
        times will
        -> give a false positive.
        ->
        -> Can anyone tell me how to ensure that one datetime is at least one
        day greater
        -> than the other?
        ->
        -> Many thanks
        ->
        -> Simon
        ->


        Maybe ?


        TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);

        if (toDate >= fromDate + oneDay)
        {
        // Code here
        }


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Comparing dates

          Michael H wrote:

          <snip>
          [color=blue]
          > TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);
          >
          > if (toDate >= fromDate + oneDay)
          > {
          > // Code here
          > }[/color]

          Unless I've misunderstood something, I believe the original purpose was
          to compare the dates - so "just before midnight" should count as a
          different date to "just after midnight", even though it wouldn't go
          inside your "if" statement.

          Jon

          Comment

          • Michael H

            #6
            Re: Comparing dates

            On 15 Feb 2006 06:18:35 -0800, "Jon Skeet [C# MVP]" <skeet@pobox.co m>
            wrote:

            -> Michael H wrote:
            ->
            -> <snip>
            ->
            -> > TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);
            -> >
            -> > if (toDate >= fromDate + oneDay)
            -> > {
            -> > // Code here
            -> > }
            ->
            -> Unless I've misunderstood something, I believe the original
            purpose was
            -> to compare the dates - so "just before midnight" should count as a
            -> different date to "just after midnight", even though it wouldn't
            go
            -> inside your "if" statement.
            ->
            -> Jon


            Jon,

            Yep, I wasn't exactly sure what he was asking specifically. Is a "1
            day" difference 24 hrs or the day's label, like Feb 12 or Feb 13 :).



            Comment

            • Simon Harvey

              #7
              Re: Comparing dates

              Thanks guys!


              Comment

              Working...