凌的博客

您现在的位置是: 首页 > 前端学习 > Js > 

Js

JS时间戳转日期格式

2016-07-01 Js 982
Date.prototype.format = function(format) { var date = { \"M+\": this.getMonth() + 1, \"d+\": this.getDate(), \"h+\": this.getHours(), \"m+\": t
Date.prototype.format = function(format) {
       var date = {
              "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+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
       }
       for (var k in date) {
              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }
       return format;
}

var newDate = new Date();
newDate.setTime(1467361131*1000);
console.log(newDate.format('yyyy-MM-dd h:m:s'))
//2016-07-01 16:18:51

文章评论

0条评论