javascript - 在所有时区中使用 JavaScript 将 Excel 日期序列号转换为日期
问题描述
我尝试使用此函数将 excel 日期转换为 javascript 日期,但它仅适用于 UTC+ 时区,不适用于 UTC-timeZone
UTC- 时区从 excel 日期开始需要一天,如果您将 25569 更改为 25568,它的工作只有 UTC- 时区
function ExcelDateToJSDate(serial) {
var utc_days = Math.floor(serial - 25569);
var utc_value = utc_days * 86400;
var date_info = new Date(utc_value * 1000);
var fractional_day = serial - Math.floor(serial) + 0.0000001;
var total_seconds = Math.floor(86400 * fractional_day);
var seconds = total_seconds % 60;
total_seconds -= seconds;
var hours = Math.floor(total_seconds / (60 * 60));
var minutes = Math.floor(total_seconds / 60) % 60;
return new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate(), hours, minutes, seconds);
}
解决方案
使用以下函数,我们可以将所有时区的 excel 日期转换为 javascript 日期。
ExcelDateToJSDate(serial) {
var hours = Math.floor((serial % 1) * 24);
var minutes = Math.floor((((serial % 1) * 24) - hours) * 60)
return new Date(Date.UTC(0, 0, serial, hours-17, minutes));
}
推荐阅读
- sql-server - 将 Azure SQL 数据库从资源组移动到另一个
- java - GridFS Java 无法连接
- java - java上的类型不匹配,无法实例化对象的类型
- docker - 在 IntelliJ IDEA 中调试并部署到远程服务器上的容器
- javascript - 当我单击按钮时,如何获取表中行的值
- java - 匹配没有 ArgumentCaptor 的可变对象
- c - 如何编写一个从迭代到迭代的字符串?
- vba - 使用 VBA 时出现类型不匹配错误
- curl - 使用 curl 将工件包上传到 sonatype:500 服务器错误
- java - 如何通过 Selenium Java 初始化 PhantomJS 浏览器