【发布时间】:2021-01-15 04:02:50
【问题描述】:
我已经通过 Node js 使用 npm 读取 Excel 文件:read-excel-file。但是日期格式是浮点数据类型。我尝试了很多方法,但有些日子有效,有些则无效。变量 _num 下的日期示例不正确。
Excel 日期数据为真:15/01/2021 10:12:16 SA 但代码返回为:2021-1-18 6:5:49
请帮帮我!!!
var _num = 44211.425405092596; // Here Excel Date data is true : 15/01/2021 10:12:16 SA but code return is : 2021-1-18 6:5:49
convertDateExcel(_num);
function convertDateExcel (__Date) {
var splitDate =String(__Date).split('.');
var _Date = new Date(1900, 0, splitDate[0]-1, 0, 0, Math.round(splitDate[1]/1157410)).getTime()/1000
const milliseconds = _Date * 1000
const dateObject = new Date(milliseconds)
var month = dateObject.getUTCMonth() + 1;
var day = dateObject.getUTCDate();
var year = dateObject.getUTCFullYear();
var time = dateObject.getHours() + ":" + dateObject.getMinutes() + ":" + dateObject.getSeconds();
console.log( year + "-" + month + "-" + day + " time : " + time)
//return year + "-" + month + "-" + day + " " + time;
}
更新 我已切换到使用 npm xlsx 而不是 read-excel-file。这可能是未转换的原始值日期。我发现 xlsx 在这方面做得更好......
【问题讨论】:
-
你为什么要创建 _Date 然后有效地为 dateObject 取整毫秒?为什么不在 _Date 上使用 setMilliseconds?为什么您使用 UTC 表示年、月和日,而使用本地时间表示时间?
-
你能帮我修复它们吗,因为我实际上是从 Stackoverflow 获得的
标签: javascript node.js excel date