momentjs 校验来自 xlsx 读取的日期数据

2024-01-24  本文已影响0人  jeneen1129

https://momentjs.com/docs/

https://www.coder.work/article/5115331#google_vignette

import moment from 'moment'


let val = dataRow[key]
let col = this.getColByKey('xxx', key) // 获取列配置
if (col.type && col.type == 'date' && col.format) { // 是日期格式
    let format = col.format.replace(/y/g, 'Y').replace(/d/g, 'D') // moment 中格式化不一样 YYYY-MM-DD HH:mm:ss
    // 有效日期
    if (moment(val, format, true).isValid() || moment(val).isValid()) {
      if (typeof val == 'number') {
        const changeDate = timeNum => {
          const d = timeNum - 1
          const t = Math.round((d - Math.floor(d)) * 24 * 60 * 60)
          return moment(new Date(1900, 0, d, 0, 0, t)).format(format)
        }
        val = changeDate(val)
      } else {
        val = moment(val).format(format)
      }
    } else {
      val = ''
    }
}
上一篇 下一篇

猜你喜欢

热点阅读