首页 > 编程语言 >JavaScript 格式化时间 《2022-10-27T11:27:04.000+00:00 -> 2022-10-27 19:27:04》

JavaScript 格式化时间 《2022-10-27T11:27:04.000+00:00 -> 2022-10-27 19:27:04》

时间:2022-10-28 10:36:20浏览次数:50  
标签:10 00 27 const 2022 date

const transformTimestamp = (timestamp) => {
  let a = new Date(timestamp).getTime();
  const date = new Date(a);
  const Y = date.getFullYear() + "-";
  const M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  const D =
    (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + "  ";
  const h =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  const m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
    ":";
  const s =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); // 秒
  const dateString = Y + M + D + h + m + s;
  // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
  return dateString;
};
console.log(transformTimestamp("2022-10-27T11:27:04.000+00:00")); //2022-10-27  19:27:04

标签:10,00,27,const,2022,date
From: https://www.cnblogs.com/windzz/p/16834971.html

相关文章