js import excel data conversion (attribute key to English_numeric date to standard time)

There are two directions to deal with

1. Excel date format conversion

2. Key to English


 Go directly to the code, everyone should understand

The following is the code that handles 1 and 2 uniformly

1 complete code

// 转换excel 文件值
    transform2AxiosExcel(results) {
      // 字段中文转英文。excel中读入的是姓名,而后端需要的是username
      // 日期处理。从excel中读入的时间是一个number值,而后端需要的是标准日期。
      const mapInfo = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName',
        聘用形式: 'formOfEmployment'
      }
      const keys = Object.keys(mapInfo)
      return results.map(item => {
        return keys.reduce((obj, key) => {
          // 判断是否是非格式化日期, 用util内的函数格式化 xxxx/xx/xx
          // 再根据接口文档 需要new Date()转成标准时间 Fri Nov 05 2021 00:00:00 GMT+0800 (中国标准时间)
          if (key === '转正日期' || key === '入职日期') {
            obj[mapInfo[key]] = new Date(formatExcelDate(item[key]))
          } else {
            obj[mapInfo[key]] = item[key]
          }
          return obj
        }, {})
      })
    },

2 The method of excel numerical date conversion in the above code

The formatExcelDate method is defined in utils/index, which is the method to convert the default excel numerical date. It uses the method of a blogger on csdn, and there is a link in it. Remember that the default format is 2023/1/1 after conversion, and it needs to be converted to Fri Nov 05 2021 00:00:00 GMT+0800 (China Standard Time) just set new Date( )

Below is the code

// excel日期处理
// 把excel文件中的日期格式的内容转回成标准时间
// https://blog.csdn.net/qq_15054679/article/details/107712966
export function formatExcelDate(numb, format = '/') {
  const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000 - 8 * 3600000)
  time.setYear(time.getFullYear())
  const year = time.getFullYear() + ''
  const month = time.getMonth() + 1 + ''
  const date = time.getDate() + ''
  if (format && format.length === 1) {
    return year + format + month + format + date
  }
  return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}

Guess you like

Origin blog.csdn.net/qq_59650449/article/details/128516295