create date object by hours and minutes












0















I want to setup a setTimeout function and need to calculate the seconds for the callback. Let's say I want to execute a function at 12:00 (HH-MM) I have to calculate the timespan up to this time. If the time has already passed the next day is relevant.



I get the current date time with new Date()



I know I can calculate the timespan in seconds by using



const difference = dateTimeOne.getTime() - dateTimeTwo.getTime();
const differenceInSeconds = difference / 1000;


Is there a way creating a second date object by passing in the hours and minutes or do I have to calculate it on my own?



An example would be new Date('12:45')










share|improve this question



























    0















    I want to setup a setTimeout function and need to calculate the seconds for the callback. Let's say I want to execute a function at 12:00 (HH-MM) I have to calculate the timespan up to this time. If the time has already passed the next day is relevant.



    I get the current date time with new Date()



    I know I can calculate the timespan in seconds by using



    const difference = dateTimeOne.getTime() - dateTimeTwo.getTime();
    const differenceInSeconds = difference / 1000;


    Is there a way creating a second date object by passing in the hours and minutes or do I have to calculate it on my own?



    An example would be new Date('12:45')










    share|improve this question

























      0












      0








      0








      I want to setup a setTimeout function and need to calculate the seconds for the callback. Let's say I want to execute a function at 12:00 (HH-MM) I have to calculate the timespan up to this time. If the time has already passed the next day is relevant.



      I get the current date time with new Date()



      I know I can calculate the timespan in seconds by using



      const difference = dateTimeOne.getTime() - dateTimeTwo.getTime();
      const differenceInSeconds = difference / 1000;


      Is there a way creating a second date object by passing in the hours and minutes or do I have to calculate it on my own?



      An example would be new Date('12:45')










      share|improve this question














      I want to setup a setTimeout function and need to calculate the seconds for the callback. Let's say I want to execute a function at 12:00 (HH-MM) I have to calculate the timespan up to this time. If the time has already passed the next day is relevant.



      I get the current date time with new Date()



      I know I can calculate the timespan in seconds by using



      const difference = dateTimeOne.getTime() - dateTimeTwo.getTime();
      const differenceInSeconds = difference / 1000;


      Is there a way creating a second date object by passing in the hours and minutes or do I have to calculate it on my own?



      An example would be new Date('12:45')







      javascript date datetime






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 13:32









      MHComputechMHComputech

      10713




      10713
























          3 Answers
          3






          active

          oldest

          votes


















          2

















          var minutes = 42;

          for (var hours = 1; hours < 24; hours+=3) {
          var newAlarm = setAlarm(hours, minutes);
          out(newAlarm)
          }



          function out(date) {
          var now = new Date()

          if (date.getDate() != now.getDate()) {
          console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
          } else {
          console.log('today: ' + date.getHours() + ":" + date.getMinutes())
          }
          }
          function setAlarm(hours, minutes) {
          var now = new Date();
          var dateTarget = new Date();

          dateTarget.setHours(hours)
          dateTarget.setMinutes(minutes)
          dateTarget.setSeconds(0)
          dateTarget.setMilliseconds(0)

          if (dateTarget < now) {
          dateTarget.setDate(dateTarget.getDate()+1)
          }
          return dateTarget
          }





          See this Documentation on MDN






          share|improve this answer


























          • almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

            – MHComputech
            Nov 22 '18 at 13:42



















          2














          You can manipulate the date and then check whether it is in the past. If it is, just add another day.






          const d = new Date();
          d.setHours(12);
          d.setMinutes(0);
          d.setSeconds(0);
          d.setMilliseconds(0);

          if (d < new Date()) {
          d.setDate(d.getDate() + 1);
          }

          console.log(d);








          share|improve this answer

































            -1














            It's possible, but you need to provide the whole time string (which we can get from calling Date() and add the missing part):



            const time = '12:45'
            const current = new Date()
            const dateTimeTwo = new Date(`${current.getFullYear()}-${current.getMonth()+1}-${current.getDate()} ${time}`)





            share|improve this answer
























            • thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

              – MHComputech
              Nov 22 '18 at 13:48











            • Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

              – str
              Nov 22 '18 at 13:58











            • That's right, sorry. @str's solution is the correct one.

              – Bitcze
              Nov 22 '18 at 14:02













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53432142%2fcreate-date-object-by-hours-and-minutes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2

















            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }





            See this Documentation on MDN






            share|improve this answer


























            • almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

              – MHComputech
              Nov 22 '18 at 13:42
















            2

















            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }





            See this Documentation on MDN






            share|improve this answer


























            • almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

              – MHComputech
              Nov 22 '18 at 13:42














            2












            2








            2










            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }





            See this Documentation on MDN






            share|improve this answer


















            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }





            See this Documentation on MDN






            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }





            var minutes = 42;

            for (var hours = 1; hours < 24; hours+=3) {
            var newAlarm = setAlarm(hours, minutes);
            out(newAlarm)
            }



            function out(date) {
            var now = new Date()

            if (date.getDate() != now.getDate()) {
            console.log('tomorrow: ' + date.getHours() + ":" + date.getMinutes())
            } else {
            console.log('today: ' + date.getHours() + ":" + date.getMinutes())
            }
            }
            function setAlarm(hours, minutes) {
            var now = new Date();
            var dateTarget = new Date();

            dateTarget.setHours(hours)
            dateTarget.setMinutes(minutes)
            dateTarget.setSeconds(0)
            dateTarget.setMilliseconds(0)

            if (dateTarget < now) {
            dateTarget.setDate(dateTarget.getDate()+1)
            }
            return dateTarget
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 14:02

























            answered Nov 22 '18 at 13:37









            yunzenyunzen

            21.2k84979




            21.2k84979













            • almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

              – MHComputech
              Nov 22 '18 at 13:42



















            • almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

              – MHComputech
              Nov 22 '18 at 13:42

















            almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

            – MHComputech
            Nov 22 '18 at 13:42





            almost, if the time has already passed I have to set the day to next day. That's why I asked if I have to do it manually

            – MHComputech
            Nov 22 '18 at 13:42













            2














            You can manipulate the date and then check whether it is in the past. If it is, just add another day.






            const d = new Date();
            d.setHours(12);
            d.setMinutes(0);
            d.setSeconds(0);
            d.setMilliseconds(0);

            if (d < new Date()) {
            d.setDate(d.getDate() + 1);
            }

            console.log(d);








            share|improve this answer






























              2














              You can manipulate the date and then check whether it is in the past. If it is, just add another day.






              const d = new Date();
              d.setHours(12);
              d.setMinutes(0);
              d.setSeconds(0);
              d.setMilliseconds(0);

              if (d < new Date()) {
              d.setDate(d.getDate() + 1);
              }

              console.log(d);








              share|improve this answer




























                2












                2








                2







                You can manipulate the date and then check whether it is in the past. If it is, just add another day.






                const d = new Date();
                d.setHours(12);
                d.setMinutes(0);
                d.setSeconds(0);
                d.setMilliseconds(0);

                if (d < new Date()) {
                d.setDate(d.getDate() + 1);
                }

                console.log(d);








                share|improve this answer















                You can manipulate the date and then check whether it is in the past. If it is, just add another day.






                const d = new Date();
                d.setHours(12);
                d.setMinutes(0);
                d.setSeconds(0);
                d.setMilliseconds(0);

                if (d < new Date()) {
                d.setDate(d.getDate() + 1);
                }

                console.log(d);








                const d = new Date();
                d.setHours(12);
                d.setMinutes(0);
                d.setSeconds(0);
                d.setMilliseconds(0);

                if (d < new Date()) {
                d.setDate(d.getDate() + 1);
                }

                console.log(d);





                const d = new Date();
                d.setHours(12);
                d.setMinutes(0);
                d.setSeconds(0);
                d.setMilliseconds(0);

                if (d < new Date()) {
                d.setDate(d.getDate() + 1);
                }

                console.log(d);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 14:00

























                answered Nov 22 '18 at 13:54









                strstr

                17.9k65679




                17.9k65679























                    -1














                    It's possible, but you need to provide the whole time string (which we can get from calling Date() and add the missing part):



                    const time = '12:45'
                    const current = new Date()
                    const dateTimeTwo = new Date(`${current.getFullYear()}-${current.getMonth()+1}-${current.getDate()} ${time}`)





                    share|improve this answer
























                    • thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                      – MHComputech
                      Nov 22 '18 at 13:48











                    • Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                      – str
                      Nov 22 '18 at 13:58











                    • That's right, sorry. @str's solution is the correct one.

                      – Bitcze
                      Nov 22 '18 at 14:02


















                    -1














                    It's possible, but you need to provide the whole time string (which we can get from calling Date() and add the missing part):



                    const time = '12:45'
                    const current = new Date()
                    const dateTimeTwo = new Date(`${current.getFullYear()}-${current.getMonth()+1}-${current.getDate()} ${time}`)





                    share|improve this answer
























                    • thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                      – MHComputech
                      Nov 22 '18 at 13:48











                    • Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                      – str
                      Nov 22 '18 at 13:58











                    • That's right, sorry. @str's solution is the correct one.

                      – Bitcze
                      Nov 22 '18 at 14:02
















                    -1












                    -1








                    -1







                    It's possible, but you need to provide the whole time string (which we can get from calling Date() and add the missing part):



                    const time = '12:45'
                    const current = new Date()
                    const dateTimeTwo = new Date(`${current.getFullYear()}-${current.getMonth()+1}-${current.getDate()} ${time}`)





                    share|improve this answer













                    It's possible, but you need to provide the whole time string (which we can get from calling Date() and add the missing part):



                    const time = '12:45'
                    const current = new Date()
                    const dateTimeTwo = new Date(`${current.getFullYear()}-${current.getMonth()+1}-${current.getDate()} ${time}`)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 13:46









                    BitczeBitcze

                    512




                    512













                    • thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                      – MHComputech
                      Nov 22 '18 at 13:48











                    • Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                      – str
                      Nov 22 '18 at 13:58











                    • That's right, sorry. @str's solution is the correct one.

                      – Bitcze
                      Nov 22 '18 at 14:02





















                    • thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                      – MHComputech
                      Nov 22 '18 at 13:48











                    • Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                      – str
                      Nov 22 '18 at 13:58











                    • That's right, sorry. @str's solution is the correct one.

                      – Bitcze
                      Nov 22 '18 at 14:02



















                    thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                    – MHComputech
                    Nov 22 '18 at 13:48





                    thanks for your answer. I think @HerrSerker provided a cleaner solution. But as I mentioned I have to consider the next day if the time has already passed

                    – MHComputech
                    Nov 22 '18 at 13:48













                    Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                    – str
                    Nov 22 '18 at 13:58





                    Note that you are not using a valid datetime string which will fail in certain browsers (e.g. Safari).

                    – str
                    Nov 22 '18 at 13:58













                    That's right, sorry. @str's solution is the correct one.

                    – Bitcze
                    Nov 22 '18 at 14:02







                    That's right, sorry. @str's solution is the correct one.

                    – Bitcze
                    Nov 22 '18 at 14:02




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53432142%2fcreate-date-object-by-hours-and-minutes%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?