Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to compare two Times [modified]

How to compare two Times [modified]

Scheduled Pinned Locked Moved C#
helptutorial
9 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VenkataRamana Gali
    wrote on last edited by
    #1

    Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

    regards GV Ramana

    M M M D L 7 Replies Last reply
    0
    • V VenkataRamana Gali

      Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

      regards GV Ramana

      M Offline
      M Offline
      Mali Perica
      wrote on last edited by
      #2

      For hours and minutes only, without dates and seconds:

              DateTime first;
              DateTime second;
      
              if (first.Hour > second.Hour)
              {
                  //first is later
              }
              else
                  if (first.Hour == second.Hour)
                      if (first.Minute > second.Minute)
                      {
                          //first is later
                      }
                      else
                          if (first.Minute == second.Minute)
                          {
                              //they are the same
                          }
                          else
                          {
                              //second is later
                          }
                  else
                  {
                      //second is later
                  }
      
      1 Reply Last reply
      0
      • V VenkataRamana Gali

        Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

        regards GV Ramana

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, Look at System.TimeSpan class. All the best, Martin

        1 Reply Last reply
        0
        • V VenkataRamana Gali

          Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

          regards GV Ramana

          M Offline
          M Offline
          Muammar
          wrote on last edited by
          #4

          TimeSpan is what you need.


          Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

          1 Reply Last reply
          0
          • V VenkataRamana Gali

            Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

            regards GV Ramana

            D Offline
            D Offline
            Dewald
            wrote on last edited by
            #5

            Yu can use the System.TimeSpan class. Quite handy, especially when used in conjunction with System.DateTime. DateTime dt1 = DateTime.Parse("2007/06/14 13:24:00"); DateTime dt2 = DateTime.Parse("2007/06/14 12:00:00"); TimeSpan ts = dt1 - dt2; double elapsed = ts.TotalHours; This will give you the total number of hours elapsed from dt1 until dt2. If you are rather interested in the total number of minutes that elapsed you can use: double elapse = ts.TotalMiutes; or a host of others.

            1 Reply Last reply
            0
            • V VenkataRamana Gali

              Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

              regards GV Ramana

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, if you want to ignore the other fields in a DateTime, you can not easily use the available functions, nor TimeSpan. I then suggest something like this:

              public static int CompareHourMinuteOnly(DateTime dt1, DateTime dt2) {
              int diff=dt1.Hour-dt2.Hour;
              if (diff==0) diff=dt1.Minute-dt2.Minute;
              return diff;
              }

              :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              1 Reply Last reply
              0
              • V VenkataRamana Gali

                Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

                regards GV Ramana

                A Offline
                A Offline
                andreumv
                wrote on last edited by
                #7

                Ramana. G.V wrote:

                how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message.

                If DateTimeObject1.TimeOfDay = DatTimeObject2.TimeOfDay Then 'do something End If

                M 1 Reply Last reply
                0
                • A andreumv

                  Ramana. G.V wrote:

                  how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message.

                  If DateTimeObject1.TimeOfDay = DatTimeObject2.TimeOfDay Then 'do something End If

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  Hmm, It's not an answere to the question, and it's not C# code!

                  1 Reply Last reply
                  0
                  • V VenkataRamana Gali

                    Hello all, how to compare two times using DateTime object. I need to compare hours & minutes only based on this i am showing some message. plz help me

                    regards GV Ramana

                    L Offline
                    L Offline
                    Le centriste
                    wrote on last edited by
                    #9

                    Another way would be to force the 2 DateTime instances to have the same date (hardcode to something like 0001-01-01) and then do a DateTime.Compare(d1, d2);

                    ----- Formerly MP(2) If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups