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. How to add TimeSpan to a DatTime

How to add TimeSpan to a DatTime

Scheduled Pinned Locked Moved C#
helptutorial
7 Posts 4 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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I want to add a TimeSpan, in milliseconds into DateTime variable. I've try this.

    DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp));

    For different calTimeStamp give the same value, that is dtStart as the output. No time is added there. Can you someone tell me where I'm going wrong. Thanks

    I appreciate your help all the time... CodingLover :)

    G D R 3 Replies Last reply
    0
    • C CodingLover

      Hi all, I want to add a TimeSpan, in milliseconds into DateTime variable. I've try this.

      DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp));

      For different calTimeStamp give the same value, that is dtStart as the output. No time is added there. Can you someone tell me where I'm going wrong. Thanks

      I appreciate your help all the time... CodingLover :)

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      1. There is an extra closing bracket. 2. As DateTime is struct dtStart will not change but da will store result of the addition.

      Giorgi Dalakishvili #region signature my articles My blog[^] #endregion

      C 1 Reply Last reply
      0
      • C CodingLover

        Hi all, I want to add a TimeSpan, in milliseconds into DateTime variable. I've try this.

        DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp));

        For different calTimeStamp give the same value, that is dtStart as the output. No time is added there. Can you someone tell me where I'm going wrong. Thanks

        I appreciate your help all the time... CodingLover :)

        D Offline
        D Offline
        David Fleming
        wrote on last edited by
        #3

        Two things: 1) Make sure calTimeStamp does not equal zero because the following works fine for me: int calTimeStamp = 30; //so that I know I have a value DateTime dtStart = DateTime.Now; //I need a start time DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp); //this adds 30 seconds to dtStart 2) Using TimeSpan(0, 0, variable) gives you seconds not milliseconds. For milliseconds you have to use TimeSpan(days, hours, seconds, milliseconds);

        C 1 Reply Last reply
        0
        • C CodingLover

          Hi all, I want to add a TimeSpan, in milliseconds into DateTime variable. I've try this.

          DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp));

          For different calTimeStamp give the same value, that is dtStart as the output. No time is added there. Can you someone tell me where I'm going wrong. Thanks

          I appreciate your help all the time... CodingLover :)

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          Is calTimeStamp the number of milliseconds , seconds, or minutes? It looks like it's seconds based on the constructor overload you used. (Your variable names kinda suck.) I always use the last overload so that there's no question about what the value represents.

          DateTime da = dtStart.Add(new TimeSpan(0,0,0,calTimeStamp,0));

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            1. There is an extra closing bracket. 2. As DateTime is struct dtStart will not change but da will store result of the addition.

            Giorgi Dalakishvili #region signature my articles My blog[^] #endregion

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            Ya, that bracket is added by mistake. Sorry about that.

            I appreciate your help all the time... CodingLover :)

            G 1 Reply Last reply
            0
            • D David Fleming

              Two things: 1) Make sure calTimeStamp does not equal zero because the following works fine for me: int calTimeStamp = 30; //so that I know I have a value DateTime dtStart = DateTime.Now; //I need a start time DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp); //this adds 30 seconds to dtStart 2) Using TimeSpan(0, 0, variable) gives you seconds not milliseconds. For milliseconds you have to use TimeSpan(days, hours, seconds, milliseconds);

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #6

              David Fleming wrote:

              1. Make sure calTimeStamp does not equal zero because the following works fine for me:

              Yep, this is the issue. I'm doing this process in a loop and each time calTimeStamp set to zero.

              I appreciate your help all the time... CodingLover :)

              1 Reply Last reply
              0
              • C CodingLover

                Ya, that bracket is added by mistake. Sorry about that.

                I appreciate your help all the time... CodingLover :)

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                Glad to help you :)

                Giorgi Dalakishvili #region signature my articles My blog[^] #endregion

                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