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
  1. Home
  2. General Programming
  3. C#
  4. Military (Midnight) DateTime subtraction

Military (Midnight) DateTime subtraction

Scheduled Pinned Locked Moved C#
helpquestiondatabase
14 Posts 7 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.
  • J Jacob D Dixon

    LOL. It is just time only. So if timeOut.Hour == 0 then I use it to add 24 hours to it then do the subtraction right

    A Offline
    A Offline
    akidan
    wrote on last edited by
    #5

    Well, except then you'll get someone poor sod who works until 1am who blows that assumption up. :) I think you can do something like

    if (timeHere < TimeSpan.Zero)
    timeHere += TimeSpan.FromDays(1);

    I think this should work, as long as nobody is working longer than 24 hours shifts. ;) I don't have a compiler in front of me, so you may want to try a few values in that and make sure it works as expected, though. :)

    1 Reply Last reply
    0
    • A akidan

      Is there an actual date associated with the value, or is it strictly time only? If it is time only, this explains why you're getting a negative value... A simple workaround may be to simply add 24 hours to your result if timeHere is negative, as you can hopefully assume nobody's time traveling and clocking out before they clock in. :)

      J Offline
      J Offline
      Jacob D Dixon
      wrote on last edited by
      #6

      Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!

      A D 2 Replies Last reply
      0
      • J Jacob D Dixon

        Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!

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

        Happy to be of assistance. :)

        1 Reply Last reply
        0
        • P paas

          In military time, 00:04 is 12:04AM. There is no such thing as 24:04 in military time; military time starts at 0000 (midnight). If you are subtracting 1:30PM from 12:04AM for the same date, you should get a timespan result of -13:26.

          J Offline
          J Offline
          Jacob D Dixon
          wrote on last edited by
          #8

          Thanks, I knew there wasnt a 24 but it doesnt work if you subtract 00 from any other number to get the correct time.. So I added 24 to the time that held the midnight number to subtract the other time.

          1 Reply Last reply
          0
          • J Jacob D Dixon

            How would I fix this problem I am having? I am reading datetime values from a database.

            DateTime timeIn = DateTime.Parse(myReader["TimeIn"].ToString());
            DateTime timeOut = DateTime.Parse(myReader["TimeOut"].ToString());

            TimeSpan timeHere = timeOut.Subtract(timeIn);

            Now this code works perfectly except for dealing with midnight. If the person is here at 1:30PM and leaves at lets say 12:04AM, then how do I calulate that? It returns an hour of -13? The time is entered at 00:04 when put in the datebase (It doesn't let you enter 24:04)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #9

            That would be why you should store the full date and time.

            L 1 Reply Last reply
            0
            • P PIEBALDconsult

              That would be why you should store the full date and time.

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

              A true evangelist would add "according to ISO 8601" :laugh:

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              modified on Friday, June 10, 2011 11:32 PM

              G P 2 Replies Last reply
              0
              • L Luc Pattyn

                A true evangelist would add "according to ISO 8601" :laugh:

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                modified on Friday, June 10, 2011 11:32 PM

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #11

                Well, not in this case, as if you would store a complete date and time you should store it as a datetime value, not a string... :)

                Despite everything, the person most likely to be fooling you next is yourself.

                L 1 Reply Last reply
                0
                • G Guffa

                  Well, not in this case, as if you would store a complete date and time you should store it as a datetime value, not a string... :)

                  Despite everything, the person most likely to be fooling you next is yourself.

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

                  True evangelists don't care about circumstances, they spread the gospel no matter what. ;P

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  modified on Friday, June 10, 2011 11:32 PM

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    A true evangelist would add "according to ISO 8601" :laugh:

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    modified on Friday, June 10, 2011 11:32 PM

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #13

                    Alas, no one listens. :sigh: And at least he was already using 24-hour time, so I didn't feel I had to invoke the gospel according to ISO 8601.

                    1 Reply Last reply
                    0
                    • J Jacob D Dixon

                      Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!

                      D Offline
                      D Offline
                      darkelv
                      wrote on last edited by
                      #14

                      Well, until there's one guy who stayed for longer than 1 day there... ;)

                      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