js时间戳转换

后台传过来的字符串1542274800000,怎么把它转换成时间格式

阅读 43k
6 个回答
放一个自己经常用的代码
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(1542274800000).Format('yy-MM-dd hh:mm:ss'); //"2018-11-15 17:40:00"

javascript时间戳转日期时间自定义格式,支持年,月,周,日,时,分,秒多种形式的组合

示例

年、月、日、时、分、秒
var date = jutils.formatDate(new Date(1533686888*1000),"YYYY-MM-DD HH:ii:ss");
console.log(date);
// 2019-07-09 19:44:01
年、月、日、周
var date = jutils.formatDate(new Date(1562672641*1000),"YYYY-MM-DD 周W");
console.log(date);
//2019-07-09 周二
月、日、周
var date = jutils.formatDate(new Date(1562672641*1000),"MM-DD 周W");
console.log(date);
//07-09 周二
时、分、秒
var date = jutils.formatDate(new Date(1562672641*1000),"HH:ii:ss");
console.log(date);
//19:44:01

参考:

一行js代码实现时间戳转时间格式

new Date(1542274800000)

new Date(timestamp)

这种问题也问吗?

moment.js
想转成什么格式都行

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题