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)
}
### 下载 Popper.min.js 文件的方法 对于希望获取 `popper.min.js` 的开发者来说,可以通过多种方式来实现这一目标。通常情况下,推荐通过官方渠道或可靠的分发网络 (CDN) 来获得最新的稳定版文件。 #### 使用 CDN 获取 Popper.min.js 最简单的方式之一是从流行的 CDN 中加载所需的 JavaScript 库。这不仅简化了集成过程,还可能提高性能,因为许多用户已经缓存了来自这些服务提供商的内容。例如: ```html <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2/dist/umd/popper.min.js"></script> ``` 这种方式不需要手动下载文件到本地服务器;只需将上述 `<script>` 标签添加至 HTML 文档中的适当位置即可立即使用 Popper 功能[^1]。 #### 从 npm 或 yarn 安装 如果项目采用模块化构建工具链,则可以直接利用包管理器如 npm 或 Yarn 进行安装。命令如下所示: ```bash npm install @popperjs/core # 或者 yarn add @popperjs/core ``` 之后可以根据具体需求引入特定功能模块,而不是整个库,从而减少打包后的体积并优化加载速度[^2]。 #### 访问 GitHub 发布页面下载压缩包 另一种方法是访问 Popper.js 的 [GitHub Releases](https://github.com/popperjs/popper-core/releases) 页面,在这里可以选择不同版本的 tarball 或 zip 归档进行下载解压操作。这种方法适合那些偏好离线工作环境或是想要定制编译选项的人群[^3]。 #### 手动克隆仓库 最后一种较为少见但也可行的办法便是直接克隆完整的 Git 存储库副本。这样可以获得开发分支以及历史记录等更多信息,适用于贡献代码或者深入学习内部机制的情况。 ```bash git clone https://github.com/popperjs/popper-core.git cd popper-core ``` 完成以上任一途径后便能成功取得所需版本的 Popper.min.js 文件,并将其应用于个人项目之中[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值