js格式化时间戳,匹配多种格式(更新:代码优化,对入参可进行指定格式)

js格式化时间戳,可以匹配多种格式

// 获取当前时间戳:
const now = new Date().getTime()
// 使用下面的函数
const time = this.timeToHourFn(now, 'yyyy-MM-dd HH:mm:ss')
console.log(time) //  // 2030-12-31 12:22:22

// time: 传入的时间戳;formatter需要的格式例如:yyyy-MM-dd HH:mm:ss
timeToHourFn(time, formatter = 'yyyy-MM-dd HH:mm:ss'){
    
    
  if(!time) return ''
  const date = new Date(time)
  const yyyy = date.getFullYear()
  const MM = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
  const M = date.getMonth() + 1
  const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const d = date.getDate()
  const HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  const H = date.getHours()
  const mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  const m = date.getMinutes()
  const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  const s = date.getSeconds()
  let timeObject = {
    
    
    yyyy: `${
      
      yyyy}`,
    MM: `${
      
      MM}`,
    dd: `${
      
      dd}`,
    HH: `${
      
      HH}`,
    mm: `${
      
      mm}`,
    ss: `${
      
      ss}`,
    M: `${
      
      M}`,
    d: `${
      
      d}`,
    H: `${
      
      H}`,
    m: `${
      
      m}`,
    s: `${
      
      s}`,
  }
  const reg = new RegExp(Object.keys(timeObject).join('|'),'g')
  const res = formatter.replace(reg, (k) => {
    
    
    return timeObject[k]
  })
  return res
}

更新:代码优化,对入参可进行指定格式

下方的代码使用方式: 

获取中国标准时间: 
console.log(timejs().t) // Fri Mar 24 2023 14:50:39 GMT+0800 (中国标准时间)

获取当前时间: 
console.log(timejs().format()) // 2023-03-24 14:51:15

按指定格式获取当前时间: 
console.log(timejs().format('yyyy.MM.dd HH.mm.ss 星期DD')) // 2023.03.24 14.56.09 星期五

通过指定时间修改时间格式: 
console.log(timejs('20220612', 'yyyyMMdd').format('yyyy-MM-dd')) // 2022-06-12

前后N天的时间: 
向前加一天:
console.log(timejs().addDay(1).format('yyyy-MM-dd')) // 2023-03-25
向前加一天并补23:59:59
console.log(timejs().addDay(1).format('yyyy-MM-dd 23:59:59')) // 2023-03-25 23:59:59
向后减一天:
console.log(timejs().addDay(-1).format('yyyy-MM-dd')) // 2023-03-23
向后减一天并补00:00:00
console.log(timejs().addDay(-1).format('yyyy-MM-dd 00:00:00')) // 2023-03-23 00:00:00

当前周的 datarange[]
当前周一至周日:
console.log(timejs().getWeek('start').format('yyyy-MM-dd 00:00:00 星期DD')) // 2023-03-20 00:00:00 星期一
console.log(timejs().getWeek('end').format('yyyy-MM-dd 23:59:59 星期DD')) // 2023-03-26 23:59:59 星期日

当前月的 datarange[]
当前月的第一天至当前月的最后一天:
console.log(timejs().getMonth('start').format('yyyy-MM-dd 00:00:00 星期DD')) // 2023-03-01 00:00:00 星期三
console.log(timejs().getMonth('end').format('yyyy-MM-dd 23:59:59 星期DD')) // 2023-03-31 23:59:59 星期五
class Time{
    
    
  constructor(m, f) {
    
    
    // 获取时间
    this.getTime(m, f)
  }
  // 06-12 MM-dd
  getTime(m, f){
    
    
    if(!m) m = new Date()
    m = m.toString()
    if(f) {
    
    
      let date = new Date()
      let timeObject = {
    
    
        yyyy: date.getFullYear(),
        MM: (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1),
        dd: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
        HH: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
        mm: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
        ss: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(),
      }
      if(f.indexOf('yyyy') !== -1) {
    
     // 有yyyy
        let i = f.indexOf('yyyy')
        timeObject = {
    
    
          yyyy: m.slice(i, i + 4),
          MM: '01',
          dd: '01',
          HH: '00',
          mm: '00',
          ss: '00',
        }
      }
      if(f.indexOf('MM') !== -1) {
    
     // 有MM
        let i = f.indexOf('MM')
        timeObject = {
    
    
          ...timeObject,
          MM: m.slice(i, i + 2),
          dd: '01',
          HH: '00',
          mm: '00',
          ss: '00',
        }
      }
      if(f.indexOf('dd') !== -1) {
    
     // 有dd
        let i = f.indexOf('dd')
        timeObject = {
    
    
          ...timeObject,
          dd: m.slice(i, i + 2),
          HH: '00',
          mm: '00',
          ss: '00',
        }
      }
      if(f.indexOf('HH') !== -1) {
    
     // 有HH
        let i = f.indexOf('HH')
        timeObject = {
    
    
          ...timeObject,
          HH: m.slice(i, i + 2),
          mm: '00',
          ss: '00',
        }
      }
      if(f.indexOf('mm') !== -1) {
    
     // 有mm
        let i = f.indexOf('mm')
        timeObject = {
    
    
          ...timeObject,
          mm: m.slice(i, i + 2),
          ss: '00',
        }
      }
      if(f.indexOf('ss') !== -1) {
    
     // 有ss
        let i = f.indexOf('ss')
        timeObject = {
    
    
          ...timeObject,
          ss: m.slice(i, i + 2),
        }
      }
      let time = timeObject.yyyy + '-' + timeObject.MM + '-' + timeObject.dd + ' ' + timeObject.HH + ':' + timeObject.mm + ':' + timeObject.ss
      console.log(time);
      this.t = new Date(time)
    } else {
    
    
      this.t = new Date(m)
    }
  }
  addDay(d = 0){
    
    
    d = parseFloat(d)
    this.t = this.t.getTime() + d * 24 * 60 * 60 * 1000
    return this
  }
  format(f = 'yyyy-MM-dd HH:mm:ss'){
    
    
    const week = ['日','一','二','三','四','五','六']
    const date = new Date(this.t)
    const yyyy = date.getFullYear()
    const MM = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
    const M = date.getMonth() + 1
    const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    const d = date.getDate()
    const HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    const H = date.getHours()
    const mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    const m = date.getMinutes()
    const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    const s = date.getSeconds()
    const DD = week[date.getDay()]
    let timeObject = {
    
    
      yyyy: `${
      
      yyyy}`,
      MM: `${
      
      MM}`,
      dd: `${
      
      dd}`,
      HH: `${
      
      HH}`,
      mm: `${
      
      mm}`,
      ss: `${
      
      ss}`,
      M: `${
      
      M}`,
      d: `${
      
      d}`,
      H: `${
      
      H}`,
      m: `${
      
      m}`,
      s: `${
      
      s}`,
      DD: `${
      
      DD}`,
    }
    const reg = new RegExp(Object.keys(timeObject).join('|'),'g')
    const res = f.replace(reg, (k) => {
    
    
      return timeObject[k]
    })
    return res
  }
  getWeek(type){
    
    
    const date = new Date(this.t)
    let w = date.getDay()
    if(w === 0) w = 7
    if(type === 'end') {
    
    
      let d = 7 - w
      this.addDay(d)
    } else if(type === 'start') {
    
    
      let d = w - 1
      this.addDay(-d)
    }
    return this
  }
  getMonth(type){
    
    
    const date = new Date(this.t)
    const year = date.getFullYear()
    const w = date.getMonth() + 1
    let n = date.getDate()
    let dif
    if([1,3,5,7,8,10,12].includes(w)) {
    
     // 31天
      dif = 31
    } else if([4,6,9,11].includes(w)) {
    
     // 30天
      dif = 30
    } else {
    
     // 2月
      dif = 28 // 平年 28 天
      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
    
     // 闰年 29 天
        dif = 29
      }
    }
    if(type === 'end') {
    
    
      let d = dif - n
      this.addDay(d)
    } else if(type === 'start') {
    
    
      let d = n - 1
      this.addDay(-d)
    }
    return this
  }
}
export function timejs(m = new Date(), f){
    
    
  return new Time(m, f)
}

猜你喜欢

转载自blog.csdn.net/qq_41329287/article/details/129183423