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              
12             if (/(y+)/i.test(format)) {
13                 format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
14             }
15          
16             for (var k in date) {
17                 if (new RegExp("(" + k + ")").test(format)) {
18                   format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
19                 }
20             }
21              
22             return format;
23         }

使用时 new Date(xx).format("yyyy-MM-dd hh:mm:ss")即可

猜你喜欢

转载自www.cnblogs.com/tele-share/p/9922450.html