javascript扩展date对象格式化日期时间输出


Date.prototype.format = function (format) {

  var o = {
    'Y+': this.getFullYear(),
    'm+': this.getMonth() + 1,
    'd+': this.getDate(),
    'H+': this.getHours(),
    'i+': this.getMinutes(),
    's+': this.getSeconds(),
  };
  for (var k in o) {
    if (new RegExp('(' + k + ')') .test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]) .substr(('' + o[k]) .length));
    }
  }
  return format;

};


var date = new Date();

alert(date.format('Y-mm-dd H:ii:ss'));

你可能感兴趣的:(javascript)