Adding days to the Date portion of DateTime throws off the Time portion












2















So I'm having an issue in Apex with the Date's addDays function. It seems that adding more than a certain amount of days throws off the time by one hour. It all seems so arbitrary so I'm wondering if this is a known thing in Salesforce or not.



DateTime aDate = System.now();
System.debug(aDate);
DateTime adjustedDate =
DateTime.newInstance(aDate.date().addDays(4), aDate.time());


System.debug(adjustedDate);



Running that in an execute anonymous box outputs the following:
2019-03-05 17:44:28
2019-03-09 17:44:28



That's all good. The same date separated by four days and the same time. Just as you'd expect. But... adding five or more days causes behavior I can't wrap my head around.



DateTime aDate = System.now();
System.debug(aDate);
DateTime adjustedDate =
DateTime.newInstance(aDate.date().addDays(5), aDate.time());
System.debug(adjustedDate);


This outputs the following:
2019-03-05 17:46:08
2019-03-10 16:46:08



It falls back by an hour. Any idea why or is this a known thing? I'm in central timezone so my time is actually -6 hours from the printed time.










share|improve this question







New contributor




Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2















    So I'm having an issue in Apex with the Date's addDays function. It seems that adding more than a certain amount of days throws off the time by one hour. It all seems so arbitrary so I'm wondering if this is a known thing in Salesforce or not.



    DateTime aDate = System.now();
    System.debug(aDate);
    DateTime adjustedDate =
    DateTime.newInstance(aDate.date().addDays(4), aDate.time());


    System.debug(adjustedDate);



    Running that in an execute anonymous box outputs the following:
    2019-03-05 17:44:28
    2019-03-09 17:44:28



    That's all good. The same date separated by four days and the same time. Just as you'd expect. But... adding five or more days causes behavior I can't wrap my head around.



    DateTime aDate = System.now();
    System.debug(aDate);
    DateTime adjustedDate =
    DateTime.newInstance(aDate.date().addDays(5), aDate.time());
    System.debug(adjustedDate);


    This outputs the following:
    2019-03-05 17:46:08
    2019-03-10 16:46:08



    It falls back by an hour. Any idea why or is this a known thing? I'm in central timezone so my time is actually -6 hours from the printed time.










    share|improve this question







    New contributor




    Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2








      So I'm having an issue in Apex with the Date's addDays function. It seems that adding more than a certain amount of days throws off the time by one hour. It all seems so arbitrary so I'm wondering if this is a known thing in Salesforce or not.



      DateTime aDate = System.now();
      System.debug(aDate);
      DateTime adjustedDate =
      DateTime.newInstance(aDate.date().addDays(4), aDate.time());


      System.debug(adjustedDate);



      Running that in an execute anonymous box outputs the following:
      2019-03-05 17:44:28
      2019-03-09 17:44:28



      That's all good. The same date separated by four days and the same time. Just as you'd expect. But... adding five or more days causes behavior I can't wrap my head around.



      DateTime aDate = System.now();
      System.debug(aDate);
      DateTime adjustedDate =
      DateTime.newInstance(aDate.date().addDays(5), aDate.time());
      System.debug(adjustedDate);


      This outputs the following:
      2019-03-05 17:46:08
      2019-03-10 16:46:08



      It falls back by an hour. Any idea why or is this a known thing? I'm in central timezone so my time is actually -6 hours from the printed time.










      share|improve this question







      New contributor




      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      So I'm having an issue in Apex with the Date's addDays function. It seems that adding more than a certain amount of days throws off the time by one hour. It all seems so arbitrary so I'm wondering if this is a known thing in Salesforce or not.



      DateTime aDate = System.now();
      System.debug(aDate);
      DateTime adjustedDate =
      DateTime.newInstance(aDate.date().addDays(4), aDate.time());


      System.debug(adjustedDate);



      Running that in an execute anonymous box outputs the following:
      2019-03-05 17:44:28
      2019-03-09 17:44:28



      That's all good. The same date separated by four days and the same time. Just as you'd expect. But... adding five or more days causes behavior I can't wrap my head around.



      DateTime aDate = System.now();
      System.debug(aDate);
      DateTime adjustedDate =
      DateTime.newInstance(aDate.date().addDays(5), aDate.time());
      System.debug(adjustedDate);


      This outputs the following:
      2019-03-05 17:46:08
      2019-03-10 16:46:08



      It falls back by an hour. Any idea why or is this a known thing? I'm in central timezone so my time is actually -6 hours from the printed time.







      apex datetime bug






      share|improve this question







      New contributor




      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 hours ago









      DylanDylan

      132




      132




      New contributor




      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Dylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          4














          Daylight saving time 2019 in central timezone will begin at



          2:00 AM on
          Sunday, March 10



          and ends at



          2:00 AM on
          Sunday, November 3



          All times are in Central Time.



          :)






          share|improve this answer
























          • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

            – Derek F
            10 hours ago











          • Wow. I completely forgot about Daylight savings time. Thank you!

            – Dylan
            10 hours ago











          • +1. Great observation on Daylight changes.

            – Jayant Das
            8 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Dylan is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f252620%2fadding-days-to-the-date-portion-of-datetime-throws-off-the-time-portion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Daylight saving time 2019 in central timezone will begin at



          2:00 AM on
          Sunday, March 10



          and ends at



          2:00 AM on
          Sunday, November 3



          All times are in Central Time.



          :)






          share|improve this answer
























          • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

            – Derek F
            10 hours ago











          • Wow. I completely forgot about Daylight savings time. Thank you!

            – Dylan
            10 hours ago











          • +1. Great observation on Daylight changes.

            – Jayant Das
            8 hours ago
















          4














          Daylight saving time 2019 in central timezone will begin at



          2:00 AM on
          Sunday, March 10



          and ends at



          2:00 AM on
          Sunday, November 3



          All times are in Central Time.



          :)






          share|improve this answer
























          • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

            – Derek F
            10 hours ago











          • Wow. I completely forgot about Daylight savings time. Thank you!

            – Dylan
            10 hours ago











          • +1. Great observation on Daylight changes.

            – Jayant Das
            8 hours ago














          4












          4








          4







          Daylight saving time 2019 in central timezone will begin at



          2:00 AM on
          Sunday, March 10



          and ends at



          2:00 AM on
          Sunday, November 3



          All times are in Central Time.



          :)






          share|improve this answer













          Daylight saving time 2019 in central timezone will begin at



          2:00 AM on
          Sunday, March 10



          and ends at



          2:00 AM on
          Sunday, November 3



          All times are in Central Time.



          :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 10 hours ago









          Aayush KAayush K

          1,11247




          1,11247













          • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

            – Derek F
            10 hours ago











          • Wow. I completely forgot about Daylight savings time. Thank you!

            – Dylan
            10 hours ago











          • +1. Great observation on Daylight changes.

            – Jayant Das
            8 hours ago



















          • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

            – Derek F
            10 hours ago











          • Wow. I completely forgot about Daylight savings time. Thank you!

            – Dylan
            10 hours ago











          • +1. Great observation on Daylight changes.

            – Jayant Das
            8 hours ago

















          In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

          – Derek F
          10 hours ago





          In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.

          – Derek F
          10 hours ago













          Wow. I completely forgot about Daylight savings time. Thank you!

          – Dylan
          10 hours ago





          Wow. I completely forgot about Daylight savings time. Thank you!

          – Dylan
          10 hours ago













          +1. Great observation on Daylight changes.

          – Jayant Das
          8 hours ago





          +1. Great observation on Daylight changes.

          – Jayant Das
          8 hours ago










          Dylan is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Dylan is a new contributor. Be nice, and check out our Code of Conduct.













          Dylan is a new contributor. Be nice, and check out our Code of Conduct.












          Dylan is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f252620%2fadding-days-to-the-date-portion-of-datetime-throws-off-the-time-portion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]