JS 时间格式化函数

JS 时间格式化函数

01 //时间格式化函数
02 Date.prototype.format = function (format) {
03     var o = {
04         "M+": this.getMonth() + 1, //month
05         "d+": this.getDate(), //day
06         "h+": this.getHours(), //hour
07         "m+": this.getMinutes(), //minute
08         "s+": this.getSeconds(), //second
09         "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
10         "S": this.getMilliseconds() //millisecond
11     }
12     if (/(y+)/.test(format)) format = format.replace(RegExp.$1,(this.getFullYear() + "").substr(4 - RegExp.$1.length));
13     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));
14     return format;
15 }

 

使用方法:

1 new Date(blog.AddTime).format('yyyy-MM-dd hh-mm-ss');

 

返回 如: 1911-11-11 00-00-00 格式

posted @ 2011-11-09 09:43  peterlee  阅读(281)  评论(0)    收藏  举报