具有可设置时间的jQuery javascript时钟?

具有可设置时间的jQuery javascript时钟?,javascript,jquery,time,clock,Javascript,Jquery,Time,Clock,我正在寻找一个简单的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 an

我正在寻找一个简单的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也可以)。

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

只需格式化你喜欢的输出,添加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();
}
//创建数字时钟
//hh:mm:ss.nnn格式的写入时间
//el是一个元素
功能时钟(el){
if(typeof el=='string')el=document.getElementById(el);
this.el=el;
}
//时钟方法
Clock.prototype={
//实用功能
addZ:函数(n){
返回n<10?'0'+n:''+n;
},
addZZ:函数(n){
返回n<10?'00'+n:n<100?'0'+n:''+n;
},
格式化时间:函数(d){
返回此.addZ(d.getHours())+
“:”+this.addZ(d.getMinutes())+
“:”+this.addZ(d.getSeconds())+
//毫秒仅用于调试,从完成的版本中删除
'.'+this.addZZ(d.getmillizes())
},
更新:函数(){
var时钟=此;
var d=新日期();
//将下一次跑步设置为刚满一秒
var interval=1020-d.getmillizes()
this.el.innerHTML=this.formatTime(d);
setTimeout(函数(){
clock.update();
}
,间隔);
}
};
//创建一个新的时钟
//el是显示文本的id或元素
功能新时钟(el){
var y=新时钟(el);
y、 更新();
}
编辑 通用日期格式函数:

将日期设置为2011年7月5日星期二上午10:31的特定函数:

var formatDate=(函数(){
//一周中的几天,零是星期天
var days=[‘星期日’、‘星期一’、‘星期二’、‘星期三’,
“星期四”、“星期五”、“星期六”];
//一年中的几个月,零是一月
var月数=['一月','二月','三月','四月',
“五月”、“六月”、“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”];
//格式化单个数字
函数addZ(n){
返回n 12?h-12:h);
返回天数[date.getDay()]+''
+d+添加顺序(d)+”
+月[date.getMonth()]+''
+date.getFullYear()+','
+h+':'
+addZ(date.getMinutes())+“”
+美联社
}
}());

那么,将日期显示为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
  }
}());