首页 > 建站教程 > JS、jQ、TS >  js-xlsx读取excel中日期格式转换问题正文

js-xlsx读取excel中日期格式转换问题

    在使用js-xlsx插件来读取excel时,会将2018/10/16这种数据读取成类似于48264的number格式,而日期通常是字符串格式。遇到这种情况,通常有两种方法:

一、将excel文件中的日期全部改成文本格式。如下图:



二、通过js对此类number格式的特殊日期进行转换:
01// excel读取2018/01/01这种时间格式是会将它装换成数字类似于46254.1545151415 numb是传过来的整数数字,format是之间间隔的符号
02formatDate(numb, format='-') {
03  if(typeof numb === 'number'){
04    const time = new Date((numb - 1) * 24 * 3600000 + 1)
05    time.setYear(time.getFullYear() - 70)
06    const year = time.getFullYear() + ''
07    const month = time.getMonth() + 1 + ''
08    const date = time.getDate() - 1 + ''
09    if (format && format.length === 1) {
10      return year + format + month + format + date
11    }
12    return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
13  }else{
14    return numb;
15  }
16},
更多关于js导入导出excel可以查看:js-xlsx