vue + Reat 日期格式化

可以在VUEReact上使用,将日期格式化xxxx年xx月xx日

  • VUE中需要放在filters中,使用方法:时间 | formatTime
  • React使用方式类似。
 filters: {
    formatTime: function(value) {
      if (value == null || value === '' || value === undefined) {
        return ''
      }
      const date = new Date(value)
      const y = date.getFullYear()
      const m = date.getMonth() + 1
      const d = date.getDate()
      return y + '年' + m + '月' + d + '日'
    },
    formatDateTime: function(value) {
      if (value == null || value === '' || value === undefined) {
        return ''
      }
      const date = new Date(value)
      const y = date.getFullYear()
      const m = date.getMonth() + 1
      const d = date.getDate()
      const h = date.getHours().toString().padStart(2, '0')
      const mm = date.getMinutes().toString().padStart(2, '0')
      const s = date.getSeconds().toString().padStart(2, '0')
      return y + '年' + m + '月' + d + '日' + h + ':' + mm + ':' + s
    }
posted @ 2022-10-18 10:08  zko  阅读(31)  评论(0编辑  收藏  举报