web导入excel数据时间转换为JS时间

问题:

从excel中读入的日期时间“变形”了。

excel时间特性:它是1900年开始,以天数计算时间到目前的总天数,以1开始为单位,以8点作为起始时间。

图片[1]-web导入excel数据时间转换为JS时间-云言博客

解决:

// 把excel文件中的日期格式的内容转回成标准时间
/**
 * excel导入插件方法:excel时间格式化
 * @param {*} numb 需要excel时间格式
 * @param {*} format 转换分隔符
 * @returns 标准时间格式
 */
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)
}

将excel表格中的中文key替换英文key

/**
 * excel导入插件方法:key中文转英文
 * @param {*} results 需要转换的数据(需要数组)
 * @param {*} maps 转换数据的中英映射关系(下边举例)
 * @param {*} times 需要格式化的excel时间属性名(英文属性名数组)
 * @returns
 */
export function transformKeys(results, maps, times = []) {
  const newArr = []
  results.forEach((item) => {
    // 替换为key是英文的新对象,添加进数组
    const _item = {}
    for (const key in item) {
      const enKey = maps[key]
      if (times.includes(enKey)) {
        // 格式化excel时间
        _item[enKey] = formatExcelDate(item[key], '-')
      } else {
        _item[enKey] = item[key]
      }
    }
    newArr.push(_item)
  })
  return newArr
}

// * @param {*} maps 转换数据的中英映射关系(下边举例)
      const changeObj = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!评论后一分钟后可见哦~~~
提交
头像

昵称

取消
昵称表情

    暂无评论内容