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. adding 24 hours onto DateTime variable

adding 24 hours onto DateTime variable

Scheduled Pinned Locked Moved C#
help
7 Posts 6 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.
  • R Offline
    R Offline
    rcwoods
    wrote on last edited by
    #1

    hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is: DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24); startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..

    D L C 3 Replies Last reply
    0
    • R rcwoods

      hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is: DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24); startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      try this instead:

      DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]);

      DateTime endTime = startTime.AddHours(24);

      Without checking the docs I strongly suspect AddHours returns a new DateTime instead of modifying the existing one.

      -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

      C 1 Reply Last reply
      0
      • R rcwoods

        hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is: DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24); startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..

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

        Hi, the DateTime methods return a new DateTime, they do not change an existing one, hence you need endTime=endTime.AddHours(24); :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        1 Reply Last reply
        0
        • D Dan Neely

          try this instead:

          DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]);

          DateTime endTime = startTime.AddHours(24);

          Without checking the docs I strongly suspect AddHours returns a new DateTime instead of modifying the existing one.

          -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          dan neely wrote:

          Without checking the docs I strongly suspect AddHours returns a new DateTime instead of modifying the existing one

          Correct - DateTime is immutable.


          Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

          1 Reply Last reply
          0
          • R rcwoods

            hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is: DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24); startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            As others have said, the methods on DateTime do not modify the object you have. That is because the DateTime object is immutable. That means that once it is created it cannot be changed, you have to create a new one. Had you done the simplest of research - like read the documentation - you would have found this out. (Hint: In pretty much all windows applications pressing F1 will bring up the documentation.)


            Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

            M 1 Reply Last reply
            0
            • C Colin Angus Mackay

              As others have said, the methods on DateTime do not modify the object you have. That is because the DateTime object is immutable. That means that once it is created it cannot be changed, you have to create a new one. Had you done the simplest of research - like read the documentation - you would have found this out. (Hint: In pretty much all windows applications pressing F1 will bring up the documentation.)


              Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

              M Offline
              M Offline
              Malcolm Smart
              wrote on last edited by
              #6

              Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)

              "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

              "This time yesterday, I still had 24 hours to meet the deadline I've just missed today."

              P 1 Reply Last reply
              0
              • M Malcolm Smart

                Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)

                "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                "This time yesterday, I still had 24 hours to meet the deadline I've just missed today."

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Malcolm Smart wrote:

                Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)

                I doubt he's up to any game beyond Snap.

                Deja View - the feeling that you've seen this post before.

                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