【问题标题】:Javascript add hours to timeJavascript增加时间
【发布时间】:2015-10-01 18:06:12
【问题描述】:

我有这个应该显示时间的 javascript 代码。有用。不过,我不能增加额外的时间。假设我想增加 1 小时。

        <script type="text/javascript">
        Date.prototype.addHours = function(h) {    
           this.setTime(this.getTime() + (h*60*60*1000)); 
           return this;   
        }
        // This function gets the current time and injects it into the DOM

        function updateClock() {
            // Gets the current time
            var now = new Date();

            // Get the hours, minutes and seconds from the current time
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();

            // Format hours, minutes and seconds
            if (hours < 10) {
                hours = "0" + hours;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (seconds < 10) {
                seconds = "0" + seconds;
            }

            // Gets the element we want to inject the clock into
            var elem = document.getElementById('clock');

            // Sets the elements inner HTML value to our clock data
            elem.innerHTML = hours + ':' + minutes + ':' + seconds;
        }
    function start(){
        setInterval('updateClock()', 200);
    }
    </script>

第一个函数计算我要添加的毫秒数,第二个函数是“实时时钟”。如何将第一个函数实现到第二个函数中,从而得到工作结果?

【问题讨论】:

标签: javascript time clock


【解决方案1】:

要添加小时,请使用setHours

// Gets the current time
var now = new Date();

console.log("actual time:", now);

now.setHours(now.getHours() + 1)

console.log("actual time + 1 hour:", now);

供参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

【讨论】:

    【解决方案2】:

    看看这个fiddle

    这里可以使用class Date的构造函数Date(milliseconds)

    这里是sn-p。

    var now = new Date();
    alert(now);
    
    var milliseconds = new Date().getTime() + (1 * 60 * 60 * 1000);
    var later = new Date(milliseconds);
    alert(later);

    【讨论】:

      【解决方案3】:

      看看这个
      fiddle here

      var todayDate = new Date();
      alert("After adding ONE hour : "+new Date(todayDate.setHours(todayDate.getHours()+1)) );

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      【解决方案4】:

      javascript date API 即将完成,现有的方法可以用来为这个 API 添加另一个功能,有人说它很繁琐但其实不然。

      为了在日期中添加方法,我们将访问此 API 的原型,

      像这样。

      Date.prototype.addTime = function(str){
          function parse(str){
              let arr = (typeof str == 'number')?[str]:str.split(":").map(t=>t.trim());
              arr[0] = arr[0] || 0;
              arr[1] = arr[1] || 0;
              arr[2] = arr[2] || 0;
              return arr
          }
          function arrToMill(arr){
              let [h,m,s] = arr;
              return (h*60*60*1000) + (m*60*1000) + (s*1000); 
          }
          let date = new Date(this.getTime());
          let parsed = parse(str);
          date.setTime(date.getTime() + arrToMill(parsed));
          return date;
      }
      

      让它摇滚。 这个函数是不可变的

      let date = new Date();
      date.addTime(1);
      date.addTime("01:00");`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-05
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多