js to convert timestamp into custom format of year, month, day, hour, minute and second (yyyy-MM-dd HH:mm:ss)

1. Custom method conversion
	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. Use (Moment.js) JavaScript date processing library conversion

Class library address:Official website
Need to install npm first: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
}

Get relative time in 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. RelativeTime StampCalculation

getTime() The method returns the number of milliseconds since January 1, 1970.
Date.now() Get the timestamp of the current time (milliseconds)
toLocaleDateString() The method can convert the Date object's 日期部分 into a string according to the local time. and return the result.

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 

You can apply the timestamp conversion method above to test whether the time is correct.
You can also use the online version of timestamp to convert to a time string: Timestamp (Unix timestamp) conversion tool - online tool

4、day

Official website
moment.js has stopped maintenance. You can use dayjs, a lightweight time operation library, instead.
Need to install npm first: 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

Guess you like

Origin blog.csdn.net/aaa123aaasqw/article/details/133948643