vue 时间戳的转化

main.js 配置全局:

Vue.filter('formatTimestamp', function (timestamp) {
  if (!timestamp) return ''
  let date
  if(timestamp.toString().length == 10) {
    date = new Date(timestamp * 1000); // 将秒转换为毫秒
  } else {
    date = new Date(timestamp); // 毫秒级不用转换
  }
  const year = date.getFullYear();
  const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
  const day = ('0' + date.getDate()).slice(-2);
  const hours = ('0' + date.getHours()).slice(-2);
  const minutes = ('0' + date.getMinutes()).slice(-2);
  const seconds = ('0' + date.getSeconds()).slice(-2);
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
})

  

posted @ 2025-05-28 14:23  小蘑菇123  阅读(18)  评论(0)    收藏  举报