[JavaScript] 格式化时间函数 →→→→→进入此内容的聊天室

来自 , 2019-05-29, 写在 JavaScript, 查看 148 次.
URL http://www.code666.cn/view/8a057268
  1. Date.prototype.format =function(format)
  2.  {
  3. var o = {
  4. "M+" : this.getMonth()+1, //month
  5. "d+" : this.getDate(), //day
  6. "h+" : this.getHours(), //hour
  7. "m+" : this.getMinutes(), //minute
  8. "s+" : this.getSeconds(), //second
  9. "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  10. "S" : this.getMilliseconds() //millisecond
  11.  }
  12. if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
  13.  (this.getFullYear()+"").substr(4- RegExp.$1.length));
  14. for(var k in o)if(new RegExp("("+ k +")").test(format))
  15.  format = format.replace(RegExp.$1,
  16.  RegExp.$1.length==1? o[k] :
  17.  ("00"+ o[k]).substr((""+ o[k]).length));
  18. return format;
  19.  }
  20.  
  21. 以上代码必须先声明,然后在使用。使用方法:
  22.  var d =new Date().format('yyyy-MM-dd');

回复 "格式化时间函数"

这儿你可以回复上面这条便签

captcha