JS 基础的时间格式转换

 1 Date.prototype.format = function (format) {
 2     var date = {
 3         "M+": this.getMonth() + 1,
 4         "d+": this.getDate(),
 5         "h+": this.getHours(),
 6         "m+": this.getMinutes(),
 7         "s+": this.getSeconds(),
 8         "q+": Math.floor((this.getMonth() + 3) / 3),
 9         "S+": this.getMilliseconds()
10     };
11     if (/(y+)/i.test(format)) {
12         format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
13     }
14     for (var k in date) {
15         if (new RegExp("(" + k + ")").test(format)) {
16             format = format.replace(RegExp.$1, RegExp.$1.length == 1
17                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
18         }
19     }
20     return format;
21 }

如何调用?

var myDatetime=new Date(parseInt(myDate.replace("/Date(", "").replace(")/", ""))).format('yyyy-MM-dd hh:mm:ss') 

 

posted @ 2017-03-22 00:28  咖啡漩涡  阅读(269)  评论(0编辑  收藏  举报