开发者

jQuery javascript clock with settable time?

I am looking for a simple jQuery clock.

There are tonnes out there, but I am looking for one where I can set the current time, and t开发者_JAVA技巧he output format.

So I want to be able to call something like

$('#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
});

Is there something like this (even raw js will do).


Creating a counter for a clock is pretty simple, you can probably write one in less time that it takes to review the answers you'll get here. Below is one I made as an example of prototype inheritance.

Just format the output however you like, add CSS to your hearts content to make it look good.

// 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();
}

Edit

A generic date format function: http://blog.stevenlevithan.com/archives/date-time-format

A specific function to format a date to be like Tuesday 05th July 2011, 10:31 am:

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
  }
}());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜