How to compare DateTime Objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • conckrish@gmail.com

    #1

    How to compare DateTime Objects?

    Hi all..

    Can anyone tell me how to compare datetime objects?I ve three
    objects namely Current date,start date and end date.. I need to check
    the current date with Start date and end date....Plz tell me how to
    compare ???

    Thanx..
    Krish

  • jesper_lofgren@yahoo.se

    #2
    Re: How to compare DateTime Objects?

    Hi Krish,

    Check out DateTime.Compar e method !

    // Jesper

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: How to compare DateTime Objects?

      <conckrish@gmai l.com> wrote:[color=blue]
      > Can anyone tell me how to compare datetime objects?I ve three
      > objects namely Current date,start date and end date.. I need to check
      > the current date with Start date and end date....Plz tell me how to
      > compare ???[/color]

      Assuming you're trying to check whether it's in the range start-end,
      just use:

      if (start <= current &&
      current <= end)
      {
      // It's in the range
      }

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

      • Jon Skeet [C# MVP]

        #4
        Re: How to compare DateTime Objects?

        <jesper_lofgren @yahoo.se> wrote:[color=blue]
        > Check out DateTime.Compar e method ![/color]

        While you certainly can use DateTime.Compar e, the code will be more
        readable if you just use the <, <=, > and >= operators.

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

        • D. Yates

          #5
          Re: How to compare DateTime Objects?

          Krish,

          In addition to the posting from the others, you might be interested to know
          that you can also get the difference between the DateTime values by
          subtracting them.

          DateTime start = DateTime.Now;
          DateTime end = DateTime.Now.Ad dDays(1);

          TimeSpan ts = end - start;


          Dave

          <conckrish@gmai l.com> wrote in message
          news:1135168452 .677710.323150@ g14g2000cwa.goo glegroups.com.. .[color=blue]
          > Hi all..
          >
          > Can anyone tell me how to compare datetime objects?I ve three
          > objects namely Current date,start date and end date.. I need to check
          > the current date with Start date and end date....Plz tell me how to
          > compare ???
          >
          > Thanx..
          > Krish
          >[/color]


          Comment

          • clintonG

            #6
            Re: How to compare DateTime Objects?

            Isn't there something to be said about the format of the dates and times
            being compared with one another or any subsequent operations performed using
            the given dates and times?

            <%= Clinton Gallagher

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1e135f 7e1b3e245298cb7 5@msnews.micros oft.com...[color=blue]
            > <jesper_lofgren @yahoo.se> wrote:[color=green]
            >> Check out DateTime.Compar e method ![/color]
            >
            > While you certainly can use DateTime.Compar e, the code will be more
            > readable if you just use the <, <=, > and >= operators.
            >
            > --
            > 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[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: How to compare DateTime Objects?

              clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=blue]
              > Isn't there something to be said about the format of the dates and
              > times being compared with one another or any subsequent operations
              > performed using the given dates and times?[/color]

              Not entirely sure what you're getting at. DateTimes aren't
              intrinsically formatted, and they're immutable. Can you elaborate?

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

              • clintonG

                #8
                Re: How to compare DateTime Objects?

                Well, I'm wondering about values represented by or returned by a current
                date, start date and end date as the OP asked about and started thinking
                about comparison methods when formatters may have been involved.

                Consider all the formatters in this cheat sheet [1] motivated me to wonder
                if there is a "transformation " methodology that has been developed or can be
                learned. How to transform from 'g' to 'r' for example?

                <%= Clinton Gallagher

                [1] http://www.dotnetjohn.com/runtime/DateFormats.aspx


                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1e13d0 4080f508ff98cb8 7@msnews.micros oft.com...[color=blue]
                > clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=green]
                >> Isn't there something to be said about the format of the dates and
                >> times being compared with one another or any subsequent operations
                >> performed using the given dates and times?[/color]
                >
                > Not entirely sure what you're getting at. DateTimes aren't
                > intrinsically formatted, and they're immutable. Can you elaborate?
                >
                > --
                > 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[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: How to compare DateTime Objects?

                  clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=blue]
                  > Well, I'm wondering about values represented by or returned by a current
                  > date, start date and end date as the OP asked about and started thinking
                  > about comparison methods when formatters may have been involved.[/color]

                  Well, I'd hope that by the time the OP has the date/times as DateTimes
                  rather than strings (if they ever were strings) they'd be correct - so
                  whatever formatting has happened before *should* be irrelevant.
                  [color=blue]
                  > Consider all the formatters in this cheat sheet [1] motivated me to wonder
                  > if there is a "transformation " methodology that has been developed or can be
                  > learned. How to transform from 'g' to 'r' for example?[/color]

                  Parse with g and reformat with r? Sounds like the easiest way to me :)

                  I get the feeling I'm still missing your point.

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

                  • clintonG

                    #10
                    Re: How to compare DateTime Objects?

                    No, I think the answer if any would be parse with g and reformat with r.
                    I've got a real problem I've discovered and nobody has been able to tell me
                    the facts yet.

                    Using the 2.0 Website Administrator Tool e (WAT) addes new users to the
                    aspnet_Users table (Membership) in GMT(00:00) when the server the WAT runs
                    on is for example in GMT(-06:00). That is WAT records the time 6 hours into
                    the future. I'll never know what time it was in the user's local time as a
                    result.

                    I need to work with GMT as it is the format required by syndicated feeds but
                    I have no experience with conversions, comparsions and magic tricks like
                    trying to determine what the local time was when a user was added to the
                    Membership database.

                    I haven't even been able to learn if the WAT is recording time as would be
                    expected as I always assumed the "standard" was to record time using the
                    time of the server or getting it from the client and using that value.

                    <%= Clinton Gallagher

                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1e14f5 fbf133640698cb9 f@msnews.micros oft.com...[color=blue]
                    > clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=green]
                    >> Well, I'm wondering about values represented by or returned by a current
                    >> date, start date and end date as the OP asked about and started thinking
                    >> about comparison methods when formatters may have been involved.[/color]
                    >
                    > Well, I'd hope that by the time the OP has the date/times as DateTimes
                    > rather than strings (if they ever were strings) they'd be correct - so
                    > whatever formatting has happened before *should* be irrelevant.
                    >[color=green]
                    >> Consider all the formatters in this cheat sheet [1] motivated me to
                    >> wonder
                    >> if there is a "transformation " methodology that has been developed or can
                    >> be
                    >> learned. How to transform from 'g' to 'r' for example?[/color]
                    >
                    > Parse with g and reformat with r? Sounds like the easiest way to me :)
                    >
                    > I get the feeling I'm still missing your point.
                    >
                    > --
                    > 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[/color]


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: How to compare DateTime Objects?

                      clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=blue]
                      > No, I think the answer if any would be parse with g and reformat with r.[/color]

                      That's what I said, isn't it?
                      [color=blue]
                      > I've got a real problem I've discovered and nobody has been able to tell me
                      > the facts yet.
                      >
                      > Using the 2.0 Website Administrator Tool e (WAT) addes new users to the
                      > aspnet_Users table (Membership) in GMT(00:00) when the server the WAT runs
                      > on is for example in GMT(-06:00). That is WAT records the time 6 hours into
                      > the future.[/color]

                      Does it actually record the time according to system local time? If so,
                      you can just use DateTime.ToUtc to get the UTC time.
                      [color=blue]
                      > I'll never know what time it was in the user's local time as a result.[/color]

                      Finding out the user's local time is always tricky.
                      [color=blue]
                      > I need to work with GMT as it is the format required by syndicated feeds but
                      > I have no experience with conversions, comparsions and magic tricks like
                      > trying to determine what the local time was when a user was added to the
                      > Membership database.
                      >
                      > I haven't even been able to learn if the WAT is recording time as would be
                      > expected as I always assumed the "standard" was to record time using the
                      > time of the server or getting it from the client and using that
                      > value.[/color]

                      I'd certainly hope it's not getting it from the client. That's a
                      terrible way of doing things - just look what happens in Usenet. It's
                      likely to be server local time.

                      The only ways I know of converting to a user's local time are:
                      1) Get the user to tell you their timezone
                      2) Use JavaScript to convert from UTC to local time

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

                      • William Stacey [MVP]

                        #12
                        Re: How to compare DateTime Objects?

                        Not exactly sure what the question is here. If the server records in GMT
                        (00:00) (i.e. UTC) that is good. It does not matter what time zone the
                        server is actually in as long as it records in UTC. So normally, all
                        clients will send time in UTC so the server does not have to know the
                        client's time zone. Then the clients can make any required conversions for
                        display based on their local time. If the server, for some reason, needs to
                        work with the client's local time (i.e. reports, etc.) then you need to get
                        (or have stored) the client's time zone so that you can make the conversion
                        from UTC to local time *for the client. I have some code that will take a
                        user's standard time zone name and convert a UTC time to a local time at the
                        server. What are you trying to do?

                        --
                        William Stacey [MVP]

                        "clintonG" <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote in message
                        news:%23vlwGN2B GHA.2920@tk2msf tngp13.phx.gbl. ..[color=blue]
                        > No, I think the answer if any would be parse with g and reformat with r.
                        > I've got a real problem I've discovered and nobody has been able to tell
                        > me the facts yet.
                        >
                        > Using the 2.0 Website Administrator Tool e (WAT) addes new users to the
                        > aspnet_Users table (Membership) in GMT(00:00) when the server the WAT runs
                        > on is for example in GMT(-06:00). That is WAT records the time 6 hours
                        > into the future. I'll never know what time it was in the user's local time
                        > as a result.
                        >
                        > I need to work with GMT as it is the format required by syndicated feeds
                        > but I have no experience with conversions, comparsions and magic tricks
                        > like trying to determine what the local time was when a user was added to
                        > the Membership database.
                        >
                        > I haven't even been able to learn if the WAT is recording time as would be
                        > expected as I always assumed the "standard" was to record time using the
                        > time of the server or getting it from the client and using that value.
                        >
                        > <%= Clinton Gallagher
                        >
                        > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                        > news:MPG.1e14f5 fbf133640698cb9 f@msnews.micros oft.com...[color=green]
                        >> clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=darkred]
                        >>> Well, I'm wondering about values represented by or returned by a current
                        >>> date, start date and end date as the OP asked about and started thinking
                        >>> about comparison methods when formatters may have been involved.[/color]
                        >>
                        >> Well, I'd hope that by the time the OP has the date/times as DateTimes
                        >> rather than strings (if they ever were strings) they'd be correct - so
                        >> whatever formatting has happened before *should* be irrelevant.
                        >>[color=darkred]
                        >>> Consider all the formatters in this cheat sheet [1] motivated me to
                        >>> wonder
                        >>> if there is a "transformation " methodology that has been developed or
                        >>> can be
                        >>> learned. How to transform from 'g' to 'r' for example?[/color]
                        >>
                        >> Parse with g and reformat with r? Sounds like the easiest way to me :)
                        >>
                        >> I get the feeling I'm still missing your point.
                        >>
                        >> --
                        >> 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[/color]
                        >
                        >[/color]


                        Comment

                        • clintonG

                          #13
                          Re: How to compare DateTime Objects?



                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          news:MPG.1e15b4 751b9e84e698cba a@msnews.micros oft.com...[color=blue]
                          > clintonG <csgallagher@RE MOVETHISTEXTmet romilwaukee.com > wrote:[color=green]
                          >> No, I think the answer if any would be parse with g and reformat with r.[/color]
                          >
                          > That's what I said, isn't it?[/color]

                          <snip />

                          Sorry to confuse. I was repeating what you said in effect agreeing to look
                          at it from a different perspective.
                          I always apprecite your comments Jon. I'm going to reply to William as he
                          indicates he has experience with the concern I have raised when using the
                          WAT.

                          <%= Clinton Gallagher



                          Comment

                          Working...