转化excel的日期格式,数据格式转换

文章介绍了如何使用JavaScript中的formatDate和formatJson函数,处理Excel表格中日期和聘用形式的格式转换,以及将表头数据与原始数据对象进行对应并调整特定字段如日期和聘用形式的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 // 转化excel的日期格式
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
// 将表头数据和数据进行对应
 // 原来的数据[{}]要导出的数据格式是[[]]
    formatJson(headers, rows) {
      return rows.map(item => {
        // item是一个对象{mobile:13212345678,username:'张三'}
        // Object.keys(headers)  ['手机号', '姓名', '入职时间', '聘用形式', '转正时间', '工号', '部门']
        return Object.keys(headers).map(key => {
          // headers[key] 得到了中文名对应的英文名
          // 需要判断 字段 时间格式和聘用形式需要处理
          if (headers[key] === 'timeOfEntry' || headers[key] === 'correctionTime') {
            // 格式化日期
            return formatDate(item[headers[key]])
          } else if (headers[key] === 'formOfEmployment') {
            // 处理聘用形式
            const obj = EmployeesEnum.hireType.find(obj => obj.id === item[headers[key]])
            return obj ? obj.value : '未知'
          }
          return item[headers[key]]
        })
        // ['13212345678','张三']
      })
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高艳艳0216

如果有用可以打赏小仙女噢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值