1、自定义方法转换
	getYMDHMS (timestamp) {
      let time = new Date(timestamp)
      let year = time.getFullYear()
      let month = time.getMonth() + 1
      let date = time.getDate()
      let hours = time.getHours()
      let minute = time.getMinutes()
      let second = time.getSeconds()

      if (month < 10) { month = '0' + month }
      if (date < 10) { date = '0' + date }
      if (hours < 10) { hours = '0' + hours }
      if (minute < 10) { minute = '0' + minute }
      if (second < 10) { second = '0' + second }
      return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    }
  
  // 使用es6的padStart()方法来补0
  getYMDHMS (timestamp) {
      let time = new Date(timestamp)
      let year = time.getFullYear()
      const month = (time.getMonth() + 1).toString().padStart(2, '0')
      const date = (time.getDate()).toString().padStart(2, '0')
      const hours = (time.getHours()).toString().padStart(2, '0')
      const minute = (time.getMinutes()).toString().padStart(2, '0')
      const second = (time.getSeconds()).toString().padStart(2, '0')

      return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    }
2、使用(Moment.js)JavaScript 日期处理类库转换

类库地址:官网
需要先npm安装:npm install moment

import moment from 'moment'
// 时间戳(毫秒)转化为标准时间格式 
export function getFullTime(timeStamp) 
{ 
   const stamp = new Date(timeStamp)
   const time = moment(stamp).format('YYYY-MM-DD HH:mm:ss')
   return time
}

moment.js中获取相对时间:

import moment from 'moment'

// 下面控制台输出的都是字符串YYYY-MM-DD格式日期类型

// 获取今天日期
console.log(moment().format('YYYY-MM-DD'))
// 获取昨天日期
console.log(moment().subtract(1, 'days').format('YYYY-MM-DD'))
// 近一周前
console.log(moment().subtract('days', 6).format('YYYY-MM-DD'))
// 获取当月一号
console.log(moment().startOf('month').format('YYYY-MM-DD'))
// 当月末尾号
console.log(moment().endOf('month').format('YYYY-MM-DD'))
// 上周开始
console.log(moment().week(moment().week() - 1).startOf('week').format('YYYY-MM-DD'))
// 上周结束
console.log(moment().week(moment().week() - 1).endOf('week').format('YYYY-MM-DD'))

// 上个月开始
console.log(moment().month(moment().month() - 1).startOf('month').format('YYYY-MM-DD'))
// 上个月结束
console.log(moment().month(moment().month() - 1).endOf('month').format('YYYY-MM-DD'))
3、相对时间戳计算

getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
Date.now() 获取当前时间的时间戳(毫秒)
toLocaleDateString() 方法可根据本地时间把 Date 对象的日期部分转换为字符串,并返回结果。

let curDate = new Date()    
// 获取当前时间的时间戳(法一)
// const now = new Date() - 0;
//当前时间,时间戳(法二)
let endTimestamp = curDate.getTime() 
// 前一天时间戳(前24小时)
let endTimestamp = curDate.getTime() - 24 * 60 * 60 * 1000 //当前时间戳(毫秒) - 1天毫秒数 = 前一天时间戳
// 当前时间的后10分钟,时间戳
let timeStamp = curDate.getTime() + 10 * 60 * 1000 
// 明天的此时,时间戳
let endTimestamp = curDate.getTime() + 24 * 60 * 60 * 1000
// 当天0点,时间戳
let endTimestamp = new Date(new Date().toLocaleDateString()).getTime() 
// 昨天0点,时间戳
let endTimestamp = new Date(new Date().toLocaleDateString()).getTime() - 24 * 60 * 60 * 1000 
// 昨天23:59:59点(当天0点 - 1秒),时间戳
let endTimestamp = new Date(new Date().toLocaleDateString()).getTime() - 1 * 1000
// 前六天0点,时间戳
let endTimestamp = new Date(new Date().toLocaleDateString()).getTime() - 24 * 60 * 60 * 1000 * 6 

可以套用上面的时间戳换算方法测试下时间对不对。
也可以用在线版的时间戳转为时间字符串: 时间戳(Unix timestamp)转换工具 - 在线工具

4、dayjs

官网
moment.js停止维护了,可以使用dayjs这个轻量的时间操作库代替。
需要先npm安装:npm install dayjs

// 假设今天是 2022/11/04 执行下面的代码

// 当前时间的Date类型
console.log(new Date()) // Fri Nov 04 2022 13:35:46 GMT+0800 (中国标准时间)
// 当前日期和时间的 Day.js 对象
console.log(dayjs()) // M {$L: 'en', $d: Fri Nov 04 2022 13:42:26 GMT+0800 (中国标准时间), $x: {…}, $y: 2022, $M: 10, …}
// 输出当前时间格式化YYYY/MM/DD字符串类型的时间
console.log(dayjs().format('YYYY/MM/DD')) // 2022/11/04

// 三年前时间的Date类型
console.log(new Date(dayjs().subtract(3, 'year'))) // Mon Nov 04 2019 13:46:23 GMT+0800 (中国标准时间)
// 三年前时间的Day.js 对象
console.log(dayjs().subtract(3, 'year')) // M {$L: 'en', $d: Mon Nov 04 2019 13:46:23 GMT+0800 (中国标准时间), $x: {…}, $y: 2019, $M: 10, …}
// 输出三年前格式化YYYY/MM/DD字符串类型时间
console.log(dayjs().subtract(3, 'year').format('YYYY/MM/DD')) // 2019/11/04

// 输出时间为2022-05-20的Day.js 对象
console.log(dayjs('2022-05-20')) // M {$L: 'en', $d: Fri May 20 2022 00:00:00 GMT+0800 (中国标准时间), $x: {…}, $y: 2022, $M: 4, …}


// 下面的语法在官网  https://dayjs.fenxianglu.cn/category/manipulate.html
// 输出7天后时间 - 格式化YYYY/MM/DD字符串类型时间
console.log(dayjs().add(7, 'day').format('YYYY/MM/DD')) // 这里使用了链式操作,如果不加.format('YYYY/MM/DD')则输出Day.js对象
// 输出7天前时间 - 格式化YYYY/MM/DD字符串类型时间
console.log(dayjs().subtract(7, 'day').format('YYYY/MM/DD')) // 2022/10/28
// 输出今年的开始时间 - 格式化YYYY/MM/DD字符串类型时间
console.log(dayjs().startOf('year').format('YYYY/MM/DD')) // 2022/01/01
// 输出这个月底时间 - 格式化YYYY/MM/DD字符串类型时间
console.log(dayjs().endOf('month').format('YYYY/MM/DD')) // 2022/11/30

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐