Fastest method of building a timestamp of the current hour












0















I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)



Currently I'm doing the following:






var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();

d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());





Is it possible to avoid the second invocation of Date?










share|improve this question



























    0















    I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)



    Currently I'm doing the following:






    var d = new Date();
    var year = d.getUTCFullYear();
    var month = d.getUTCMonth();
    var day = d.getUTCDate();
    var hour = d.getUTCHours();

    d = new Date(year, month, day, hour);
    console.log(d);
    console.log(d.getTime());





    Is it possible to avoid the second invocation of Date?










    share|improve this question

























      0












      0








      0








      I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)



      Currently I'm doing the following:






      var d = new Date();
      var year = d.getUTCFullYear();
      var month = d.getUTCMonth();
      var day = d.getUTCDate();
      var hour = d.getUTCHours();

      d = new Date(year, month, day, hour);
      console.log(d);
      console.log(d.getTime());





      Is it possible to avoid the second invocation of Date?










      share|improve this question














      I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)



      Currently I'm doing the following:






      var d = new Date();
      var year = d.getUTCFullYear();
      var month = d.getUTCMonth();
      var day = d.getUTCDate();
      var hour = d.getUTCHours();

      d = new Date(year, month, day, hour);
      console.log(d);
      console.log(d.getTime());





      Is it possible to avoid the second invocation of Date?






      var d = new Date();
      var year = d.getUTCFullYear();
      var month = d.getUTCMonth();
      var day = d.getUTCDate();
      var hour = d.getUTCHours();

      d = new Date(year, month, day, hour);
      console.log(d);
      console.log(d.getTime());





      var d = new Date();
      var year = d.getUTCFullYear();
      var month = d.getUTCMonth();
      var day = d.getUTCDate();
      var hour = d.getUTCHours();

      d = new Date(year, month, day, hour);
      console.log(d);
      console.log(d.getTime());






      javascript datetime






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 23:32









      cdarwincdarwin

      1,51272952




      1,51272952
























          2 Answers
          2






          active

          oldest

          votes


















          1














          If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:



          var d = new Date();
          d.setMinutes(0,0);
          console.log(d);
          console.log(d.getTime());


          You could make it a oneliner, since setMinutes() already returns a timestamp:



          var timestamp = new Date().setMinutes(0,0);





          share|improve this answer


























          • Is there a way to avoid calling date completely, if I start from a timestamp?

            – cdarwin
            Nov 14 '18 at 23:42











          • You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

            – Jeff
            Nov 14 '18 at 23:44



















          0














          Not sure why you're doing that twice, you really don't need to.






          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );








          share|improve this answer
























          • I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

            – cdarwin
            Nov 14 '18 at 23:40











          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%2f53310337%2ffastest-method-of-building-a-timestamp-of-the-current-hour%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:



          var d = new Date();
          d.setMinutes(0,0);
          console.log(d);
          console.log(d.getTime());


          You could make it a oneliner, since setMinutes() already returns a timestamp:



          var timestamp = new Date().setMinutes(0,0);





          share|improve this answer


























          • Is there a way to avoid calling date completely, if I start from a timestamp?

            – cdarwin
            Nov 14 '18 at 23:42











          • You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

            – Jeff
            Nov 14 '18 at 23:44
















          1














          If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:



          var d = new Date();
          d.setMinutes(0,0);
          console.log(d);
          console.log(d.getTime());


          You could make it a oneliner, since setMinutes() already returns a timestamp:



          var timestamp = new Date().setMinutes(0,0);





          share|improve this answer


























          • Is there a way to avoid calling date completely, if I start from a timestamp?

            – cdarwin
            Nov 14 '18 at 23:42











          • You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

            – Jeff
            Nov 14 '18 at 23:44














          1












          1








          1







          If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:



          var d = new Date();
          d.setMinutes(0,0);
          console.log(d);
          console.log(d.getTime());


          You could make it a oneliner, since setMinutes() already returns a timestamp:



          var timestamp = new Date().setMinutes(0,0);





          share|improve this answer















          If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:



          var d = new Date();
          d.setMinutes(0,0);
          console.log(d);
          console.log(d.getTime());


          You could make it a oneliner, since setMinutes() already returns a timestamp:



          var timestamp = new Date().setMinutes(0,0);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 23:47

























          answered Nov 14 '18 at 23:41









          JeffJeff

          6,34911025




          6,34911025













          • Is there a way to avoid calling date completely, if I start from a timestamp?

            – cdarwin
            Nov 14 '18 at 23:42











          • You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

            – Jeff
            Nov 14 '18 at 23:44



















          • Is there a way to avoid calling date completely, if I start from a timestamp?

            – cdarwin
            Nov 14 '18 at 23:42











          • You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

            – Jeff
            Nov 14 '18 at 23:44

















          Is there a way to avoid calling date completely, if I start from a timestamp?

          – cdarwin
          Nov 14 '18 at 23:42





          Is there a way to avoid calling date completely, if I start from a timestamp?

          – cdarwin
          Nov 14 '18 at 23:42













          You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

          – Jeff
          Nov 14 '18 at 23:44





          You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.

          – Jeff
          Nov 14 '18 at 23:44













          0














          Not sure why you're doing that twice, you really don't need to.






          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );








          share|improve this answer
























          • I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

            – cdarwin
            Nov 14 '18 at 23:40
















          0














          Not sure why you're doing that twice, you really don't need to.






          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );








          share|improve this answer
























          • I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

            – cdarwin
            Nov 14 '18 at 23:40














          0












          0








          0







          Not sure why you're doing that twice, you really don't need to.






          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );








          share|improve this answer













          Not sure why you're doing that twice, you really don't need to.






          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );








          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );





          var d = new Date();

          console.log(d.getHours());
          // or
          console.log(d.getTime() );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 23:36









          SnowmonkeySnowmonkey

          3,07211012




          3,07211012













          • I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

            – cdarwin
            Nov 14 '18 at 23:40



















          • I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

            – cdarwin
            Nov 14 '18 at 23:40

















          I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

          – cdarwin
          Nov 14 '18 at 23:40





          I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour

          – cdarwin
          Nov 14 '18 at 23:40


















          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%2f53310337%2ffastest-method-of-building-a-timestamp-of-the-current-hour%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma