美文网首页
momentjs 校验来自 xlsx 读取的日期数据

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

作者: jeneen1129 | 来源:发表于2024-01-24 14:03 被阅读0次

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 = ''
    }
}

相关文章

网友评论

      本文标题:momentjs 校验来自 xlsx 读取的日期数据

      本文链接:https://www.haomeiwen.com/subject/koqmodtx.html