关于解析Excel中的日期出现是数字序列的问题

使用xlsx.js读取Excel中的日期有两种方式:

  1. XLSX.read(file, { type: 'binary' })

  2. XLSX.read(file, { type: 'binary', cellDates: true })

第一种方式得到的是一个 1900-01-01 00:00:00 开始的序列号。

复制代码
export function formatExcelDate(excelSerialDate) {
  //  25569 是 1970-01-01 对应的 Excel 序列号, Excel 时间起点:1900-01-01 00:00:00 , JS 时间起点:1970-01-01 00:00:00
  // 86400 * 1000 = 一天的毫秒数
  // console.log(new Date(Math.round((excelSerialDate - 25569) * 86400 * 1000)))
  return new Date(Math.round((excelSerialDate - 25569) * 86400 * 1000));
}

1. 为什么需要 excelSerialDate - 25569

excelSerialDate - 25569 目的是得到1970年1月1日至被计算的日期(excelSerialDate)相差了多少天。

2. 25569 表示1900-01-01 到 1970-01-01 有多少天,如何计算的

70年 × 365天 = 25550 天

加上闰年(1904,1908,...,1968)共 17 天

加上 Excel 闰年 bug(1900被误认为闰年)1 天

加上第1天从1开始而不是0的偏移 1 天

总计:25550 + 17 + 1 + 1 = 25569 天

3. Math.round 的作用

由于JS 使用 IEE 754 双精度浮点数表示数字,有些乘法会产生不准确的结果。

例如: 0.1 + 0.2 = 0.30000000000000004

为了解决精度问题,所以使用 Math.round 四舍五入。

4. 为什么需要转成 JS Date

JS Date 使用毫秒时间戳,所以使用 excelSerialDate - 25569 计算出从 JS 时间起点(1970-01-01)到目标日期的天数,然后乘以一天的毫秒数就得到了 JS 时间戳。

测试

复制代码
// 使用示例
const excelDate = 46170;
const jsDate = excelDateToJSDate(excelDate);
console.log(jsDate); // 2026-05-28T00:00:00.000Z

第二种方式得到是一个Date类型的数据

相关推荐
asdzx671 小时前
Python TXT 转 Excel (自动识别分隔符)
python·excel
也要大步向前呀1 小时前
excel(1)将多行内容合并到一行里
excel
Non-existent9874 小时前
海拔批量查询 + 批量 KML 生成工具-WPS 插件 TableGIS 新功能
javascript·c++·excel·wps
Metaphor6921 天前
使用 Python 在 Excel 文件中查找并替换
python·excel
xiaomin-Michael1 天前
EXCEL pivot table
excel
包子源1 天前
PDF 转 Word/Excel 全链路实战:Next.js + 阿里云文档智能
pdf·word·excel
Java知识技术分享1 天前
excel动态表头实现+异步使用
java·spring·excel
tedcloud1232 天前
academic-research-skills部署教程:构建AI辅助科研环境
服务器·人工智能·word·excel·dreamweaver
wtsolutions2 天前
Sheet-to-Doc Excel Add-in Released, Install Directly from Microsoft AppSource
microsoft·excel