你的位置:首页 > 软件开发 > Java > js,将日期时分秒等格式化和转化

js,将日期时分秒等格式化和转化

发布时间:2015-12-24 12:00:06
1.将js Date对象格式化为指定格式,添加一个原型方法/** * 返回指定format的string * format eg:yyyy-MM-dd hh:mm:ss **/Date.prototype.format = function(format) { var o ...

 

1.将js Date对象格式化为指定格式,添加一个原型方法

/** * 返回指定format的string * format eg:'yyyy-MM-dd hh:mm:ss' **/Date.prototype.format = function(format) {  var o = {    "M+": this.getMonth() + 1,    "d+": this.getDate(),    "h+": this.getHours(),    "m+": this.getMinutes(),    "s+": this.getSeconds(),    "q+": Math.floor((this.getMonth() + 3) / 3),    "S": this.getMilliseconds()  }  if (/(y+)/.test(format)) {    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  }  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;}使用如:new Date().format('yyyy-MM-dd');  // echo: '2015-12-01'

原标题:js,将日期时分秒等格式化和转化

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。