js 将时间戳格式化为 yyyy-MM-dd HH:mm:ss 格式的字符串

来源:互联网 发布:感到可怕的案件知乎 编辑:程序博客网 时间:2024/04/29 04:27
// js 格式化时间戳,显示格式 yyyy-MM-dd HH:mm:ss 。输入的 timestamp 例如 1510452672000
function formatTimestamp( timestamp ) {
    var dateObj = new Date( timestamp );
    
    var year = dateObj.getYear() + 1900;
    var month = dateObj.getMonth() + 1;
    var theDate = dateObj.getDate();
    var hour = dateObj.getHours();
    var minute = dateObj.getMinutes();
    var second = dateObj.getSeconds();
    return year +"-"+ month +"-" + theDate + " "+ hour +":"+ minute +":"+ second;     
}
0 0
原创粉丝点击