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



























    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

























      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














      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













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 5 at 18:01









      DylanDylan

      233




      233






















          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
            Mar 5 at 18:11











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

            – Dylan
            Mar 5 at 18:14











          • +1. Great observation on Daylight changes.

            – Jayant Das
            Mar 5 at 20:17











          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
          });


          }
          });














          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
            Mar 5 at 18:11











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

            – Dylan
            Mar 5 at 18:14











          • +1. Great observation on Daylight changes.

            – Jayant Das
            Mar 5 at 20:17
















          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
            Mar 5 at 18:11











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

            – Dylan
            Mar 5 at 18:14











          • +1. Great observation on Daylight changes.

            – Jayant Das
            Mar 5 at 20:17














          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 Mar 5 at 18:07









          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
            Mar 5 at 18:11











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

            – Dylan
            Mar 5 at 18:14











          • +1. Great observation on Daylight changes.

            – Jayant Das
            Mar 5 at 20:17



















          • 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
            Mar 5 at 18:11











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

            – Dylan
            Mar 5 at 18:14











          • +1. Great observation on Daylight changes.

            – Jayant Das
            Mar 5 at 20:17

















          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
          Mar 5 at 18:11





          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
          Mar 5 at 18:11













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

          – Dylan
          Mar 5 at 18:14





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

          – Dylan
          Mar 5 at 18:14













          +1. Great observation on Daylight changes.

          – Jayant Das
          Mar 5 at 20:17





          +1. Great observation on Daylight changes.

          – Jayant Das
          Mar 5 at 20:17


















          draft saved

          draft discarded




















































          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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents