js中date时间转换yyyy-mm-dd hh:MM:ss等格式字符串

js中date时间转换yyyyDate.prototype.format = function (format) {
           var args = {
               "M+": this.getMonth() + 1,
               "d+": this.getDate(),
               "h+": this.getHours(),
               "m+": this.getMinutes(),
               "s+": this.getSeconds(),
               "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
               "S": this.getMilliseconds()
           };
           if (/(y+)/.test(format))
               format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
           for (var i in args) {
               var n = args[i];
               if (new RegExp("(" + i + ")").test(format))
                   format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
           }
           return format;
       };
alert(new Date( 'Sun May 27 2018 09:08:09 GMT+0800').format("yyyy-MM-dd hh:mm:ss"));
-mm-dd hh:MM:ss等格式字符串

花了很长时间做出来的,通过这个问题深深的感受到:人生不易,高前端技术更不易 哈哈

猜你喜欢

转载自www.cnblogs.com/mahmud/p/11913043.html