我有一个5位数的数字37988,我想用Javascript把它转换成一个日期
我试着搜索相关方法。但是找不到
credit: https://github.com/date-fns/date-fns/issues/369
const convertDateExcel = (excelTimestamp) => {
// 1. Subtract number of days between Jan 1, 1900 and Jan 1, 1970, plus 1 (Google "excel leap year bug")
// 2. Convert to milliseconds.
const secondsInDay = 24 * 60 * 60;
const excelEpoch = new Date(1899, 11, 31);
const excelEpochAsUnixTimestamp = excelEpoch.getTime();
const missingLeapYearDay = secondsInDay * 1000;
const delta = excelEpochAsUnixTimestamp - missingLeapYearDay;
const excelTimestampAsUnixTimestamp = excelTimestamp * secondsInDay * 1000;
const parsed = excelTimestampAsUnixTimestamp + delta;
return isNaN(parsed) ? null : new Date(parsed);
};
console.log(
convertDateExcel(37988)
);
// Date Thu Jan 01 2004