vue/html 日期与时间戳互相转换

1、将日期转换成时间戳

let time = parseInt(new Date().getTime() / 1000);
console.log(time)

2、接口传过来的UTC日期和13位时间戳格式化

const formateDate = (time: any, isUtc = false) => {
  let date;
  if(isUtc){
    date = new Date(time * 1); // 13位时间戳乘1转数字
  }else{
    // UTC + 8小时
    date = new Date(+new Date(time) + 8 * 3600 * 1000);
  }
  
  let y = date.getFullYear(); //获取年份
  let month = date.getMonth() + 1; //获取月份
  let m = month < 10 ? "0" + month : month; //月份不满10天显示前加0
  let day = date.getDate(); //获取日期
  let d = day < 10 ? "0" + day : day; //日期不满10天显示前加0
  let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //小时
  let mm =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //
  let ss =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //
  time = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + ss;
  return time;
}

 

posted @ 2023-03-24 17:32  糖糖Y  阅读(1745)  评论(0编辑  收藏  举报