简单JS 日期转时间戳/时间戳转日期

这里我的后端需要秒级  毫秒级不除1000即可

// 日期转时间戳
    toStamp(date) {
      const myDate = new Date(date)
      const stmapEg = Date.parse(myDate) / 1000
      return stmapEg
    },
    // 时间戳转日期  年月日
    toDate(stamp) {
      const myStamp = new Date(stamp * 1000)
      const YY = myStamp.getFullYear()
      const MM = myStamp.getMonth() + 1 < 10 ? '0' + (myStamp.getMonth() + 1) : (myStamp.getMonth() + 1)
      const DD = myStamp.getDate() < 10 ? '0' + myStamp.getDate() : myStamp.getDate()
      const dateEg = YY + '-' + MM + '-' + DD
      return dateEg
    },
    // 时间戳转日期  年月日 时分秒
    toDateDetail(stamp) {
      const myStamp = new Date(stamp * 1000)
      const YY = myStamp.getFullYear()
      const MM = myStamp.getMonth() + 1 < 10 ? '0' + (myStamp.getMonth() + 1) : (myStamp.getMonth() + 1)
      const DD = myStamp.getDate() < 10 ? '0' + myStamp.getDate() : myStamp.getDate()
      const hh = myStamp.getHours() < 10 ? '0' + myStamp.getHours() : myStamp.getHours()
      const mm = myStamp.getMinutes() < 10 ? '0' + myStamp.getMinutes() : myStamp.getMinutes()
      const ss = myStamp.getSeconds() < 10 ? '0' + myStamp.getSeconds() : myStamp.getSeconds()
      const dateEg = YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss
      return dateEg
    }

 

posted on 2022-12-15 16:51  贲风  阅读(616)  评论(0)    收藏  举报