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

在使用js-xlsx插件来读取excel时,会将2020/04/03 14:01
这种数据自动装换成3924.584027778

所以需要自己手动再转换回来

// excel读取2020/04/03 14:01这种时间格式是会将它装换成数字类似于3924.584027778 numb是传过来的整数数字,format是之间间隔的符号
   formatExcelDate(numb, format) {
        const time = new Date((numb - 1) * 24 * 3600000 + 1);
        time.setYear(time.getFullYear() - 70);
        time.setHours(time.getHours() - 8);
        const year = time.getFullYear() + '';
        const month = time.getMonth() + 1 + '';
        const date = time.getDate() - 1 + '';
        const hours = time.getHours().toLocaleString();
        const minutes = time.getMinutes();
        if (format && format.length === 1) {
          return year + format + month + format + date + ' ' + hours + ':' + minutes;
        }
        return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date);
      }, 
      console.log(formatDate(3924.584027778, '-')) // 2020-04-03 14:01

你可能感兴趣的:(js)