【问题标题】:jQuery javascript clock with settable time?具有可设置时间的jQuery javascript时钟?
【发布时间】:2011-07-04 22:39:59
【问题描述】:

我正在寻找一个简单的 jQuery 时钟。

那里有很多吨,但我正在寻找一个可以设置当前时间和输出格式的工具。

所以我希望能够调用类似的东西

$('#clock').clock({
  format: 'l dS F Y, h:i a',    //PHP date format, but anything that can mimic this output is good
  date:   '2012-07-01 23:59:59' //MYSQL date format, but can output as anything
});

有没有这样的东西(即使是原始的js也可以)。

【问题讨论】:

    标签: javascript jquery time clock


    【解决方案1】:

    为时钟创建一个计数器非常简单,您可能可以用更少的时间编写一个计数器来查看您将在此处获得的答案。下面是我做的一个原型继承的例子。

    只需根据您的喜好格式化输出,将 CSS 添加到您的心脏内容中以使其看起来不错。

    // Create a digital clock
    // Write time in hh:mm:ss.nnn format
    // el is an element
    function Clock(el) {
      if (typeof el == 'string') el = document.getElementById(el);
      this.el = el;
    }
    
    // Clock methods
    Clock.prototype = {
      // Utilty functions
      addZ: function(n) {
        return n < 10? '0' + n : '' + n;
      },
      addZZ: function(n) {
        return n < 10? '00' + n : n < 100? '0' + n : '' + n;
      },
    
      formatTime: function(d) {
        return  this.addZ(d.getHours()) + 
          ':' + this.addZ(d.getMinutes()) +
          ':' + this.addZ(d.getSeconds()) + 
          // Milliseconds are just for debug, remove from finished version
          '.' + this.addZZ(d.getMilliseconds())
      },
    
      update: function() {
        var clock = this;
        var d = new Date();
    
        // Set next run to just after full second
        var interval = 1020 - d.getMilliseconds()
        this.el.innerHTML = this.formatTime(d);
        setTimeout(function(){
          clock.update();
        }
        ,interval);
      }
    };
    
    // Create a new clock
    // el is id or element to display text in
    function newClock(el) {
      var y = new Clock(el);
      y.update();
    }
    

    编辑

    一个通用的日期格式函数:http://blog.stevenlevithan.com/archives/date-time-format

    将日期格式化为 2011 年 7 月 5 日星期二,上午 10:31 的特定函数

    var formatDate = (function() {
    
      // Days of the week, zero is Sunday
      var days = ['Sunday','Monday','Tuesday','Wednesday',
                  'Thursday','Friday','Saturday'];
    
      // Months of the year, zero is January
      var months = ['January','February','March','April',
                    'May','June','July','August','September',
                    'October','November','December'];
    
      // Format single digit numbers
      function addZ(n) {
        return n<10? '0' + n : '' + n;
      }
    
      // Add ordinal to numbers
      function addOrdinal(n) {
        return ['th','st','nd','rd'][(''+n).slice(-1)] || 'th';
      }
    
      return function (date) {
        var d = addZ(date.getDate());
        var h = date.getHours();
        var ap = h < 12? 'am' : 'pm';
            h = addZ(h > 12? h - 12 : h);
    
        return days[date.getDay()] + ' '
          + d + addOrdinal(d)  + ' '
          + months[date.getMonth()] + ' '
          + date.getFullYear() + ', '
          + h + ':'
          + addZ(date.getMinutes()) + ' '
          + ap
      }
    }());
    

    【讨论】:

    • 那么如何将日期显示为例如Tuesday 05th July 2011, 10:31 am 实际上只显示时间是简单的部分,日期是我有点搞砸的地方。
    猜你喜欢
    • 2016-08-31
    • 2018-12-10
    • 1970-01-01
    • 2022-01-25
    • 2019-01-30
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多