时间戳的转换

1.时间戳转化为yyyy-MM-dd hh:mm:ss格式

function formatDateTime(timestamp) {
         var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
         var y = date.getFullYear();
         var m = date.getMonth() + 1;
         m = m < 10 ? ('0' + m) : m;
         var d = date.getDate();
         d = d < 10 ? ('0' + d) : d;
         var h = date.getHours();
         h = h < 10 ? ('0' + h) : h;
         var minute = date.getMinutes();
         var second = date.getSeconds();
         minute = minute < 10 ? ('0' + minute) : minute;
         second = second < 10 ? ('0' + second) : second;
         return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
 };

2.当前时间生成时间戳

var timestamp = Date.parse(new Date());
console.log(timestamp);

console.log(new Date().getTime());

你可能感兴趣的:(js)