中文转英文

689 阅读1分钟

格式化时间

// utils/index.js
// 把excel文件中的日期格式的内容转回成标准时间
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)
}

中文转英文

transExcel(data) {
      // 2.将表格的数据格式化  中文的数据转化为英文的数据
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }

      return data.map(item => {
        const enObj = {}
        const zhObjKeys = Object.keys(item) // 拿到所有的中文键

        zhObjKeys.forEach(zhKeys => {
          const enKeys = mapInfo[zhKeys]

          // 格式化时间
         if (enKeys === 'timeOfEntry' || enKeys === 'correctionTime'){
            enObj[enKeys] = formatExcelDate(item[zhKeys])
          } else {
            enObj[enKeys] = item[zhKeys]
          }
        })
        return enObj
      })
    }