"2018-12-02 11:55:48"这个可以转换成 12月2日 11:55吗

问题描述 :将时间戳转换成 12月2日 11:55-11:56 这种格式
(时间戳:1543722948000 ,1543722986000)

阅读 1.6k
2 个回答
function addZero (str) {
  str = '' + str;
  if (str.length < 2) return '0' + str;
  else return str;
}
var date = new Date(1543722948000);
var str = addZero(date.getMonth() + 1) + '月' + addZero(date.getDate()) + '日 ' + addZero(date.getHours()) + ':' + addZero(date.getMinutes());
console.log(str);
//日期格式化
Date.prototype.Format = function (fmt) {
    var o = {
            "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+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
new Date(1543722948000).Format('MM月dd日 hh:mm');  //"12月02日 11:55"
new Date(1543722986000).Format('MM月dd日 hh:mm');  //"12月02日 11:56"
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题