美文网首页
js将秒转化为年月日时分秒

js将秒转化为年月日时分秒

作者: WangYatao | 来源:发表于2019-01-09 19:30 被阅读10次

js代码

function getDateStr(seconds){
    var date = new Date(seconds*1000)
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var currentTime = year + "-" + month + "-" + day + "  " + hour + ":" + minute + ":" + second;
        return currentTime
}

调用

getDateStr(1547033457)

效果

image.png

相关文章

网友评论

      本文标题:js将秒转化为年月日时分秒

      本文链接:https://www.haomeiwen.com/subject/zbzgrqtx.html