Typesrcipt日期时间

1.获取时间戳(秒)

new Date().getTime()/1000
Date.now()/1000

 2.日期时间格式化

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('-') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

let d=formatTime(new Date())
console.log(d) //2022-01-04 11:11:08
原文地址:https://www.cnblogs.com/boye169/p/15761780.html