1. 程式人生 > >js將時間戳轉換成日期格式-陳遠波

js將時間戳轉換成日期格式-陳遠波

var timestamp =1539598555000;//時間戳

//時間戳轉換成time格式
function timestampToTime(timestamp) { var date = new Date(timestamp );//時間戳為10位需*1000,時間戳為13位的話不需乘1000 Y = date.getFullYear() + '-'; M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; D = date.getDate() + ' '; h
= date.getHours() + ':'; m = date.getMinutes() + ':'; s = date.getSeconds(); return Y+M+D+h+m+s; }
var dateTime =
timestampToTime(timestamp)//轉換時間戳
alert(dateTime);

 效果如下: