You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript将Excel日期序列号44567转换为dd/mm/YYYY格式

解决方案:Excel日期数值转JS的dd/mm/YYYY格式

你的代码存在两个关键问题:

  1. 直接将Excel日期数值传入Date构造函数会被当成1970年1月1日之后的毫秒数,而非Excel的日期天数,导致日期完全错误。
  2. JavaScript的Date对象没有toDate()方法,这是语法错误。

正确的转换步骤

Excel的日期数值是从1900年1月1日开始累计的天数,但Excel存在一个历史bug:错误将1900年判定为闰年,因此需要对数值≥60的情况做修正。

1. 转换Excel日期数值为JS Date对象

function excelToJsDate(excelDate) {
    // 修正1900年闰年bug:数值≥60时减1
    const offset = excelDate >= 60 ? 1 : 0;
    return new Date(1900, 0, excelDate - offset);
}

2. 格式化为dd/mm/YYYY字符串

自己实现格式化逻辑可以避免地区设置影响:

function formatDDMMYYYY(date) {
    const day = String(date.getDate()).padStart(2, '0');
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需加1
    const year = date.getFullYear();
    return `${day}/${month}/${year}`;
}

组合使用

const excelDate = 44567;
const jsDate = excelToJsDate(excelDate);
const formattedDate = formatDDMMYYYY(jsDate);
console.log(formattedDate); // 输出:06/01/2022

也可以合并为一个函数:

function convertExcelDate(excelDate) {
    const offset = excelDate >= 60 ? 1 : 0;
    const date = new Date(1900, 0, excelDate - offset);
    const day = String(date.getDate()).padStart(2, '0');
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const year = date.getFullYear();
    return `${day}/${month}/${year}`;
}

console.log(convertExcelDate(44567)); // 06/01/2022

内容的提问来源于stack exchange,提问作者OUED

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:45:20