Use the vue filter to modify the time format and convert '2022-06-29T07:36:12.710Z' to '2022-06-29 15:36:12'

The automatically generated time and date format is often not what we need, so format conversion is required

The implementation method uses the filter of vue, which can also be applied in js

//定义全局过滤器,originVal是传过来的值
Vue.filter('dateFormat',function(originVal){
    //利用Data进行格式转换
  const dt = new Date(originVal)

    //获取年份
  const y = dt.getFullYear()

    //获取月份,个位数的月份显示为 01,02这种样式
  const m = (dt.getMonth() + 1 + '').padStart(2,'0')

    //获取日期,个位数的日期显示为 01,02这种样式
  const d = (dt.getDate() + '').padStart(2,'0')
    
    //获取小时,个位数的小时显示为 01,02这种样式
  const hh = (dt.getHours() + '').padStart(2,'0')

    //获取分钟,个位数的分钟显示为 01,02这种样式
  const mm = (dt.getMinutes() + '').padStart(2,'0')

    //获取毫秒,个位数的小时显示为 01,02这种样式
  const ss = (dt.getSeconds() + '').padStart(2,'0')
    //进行拼接返回
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

Guess you like

Origin blog.csdn.net/weixin_65565362/article/details/125528089
Recommended