首页 > 解决方案 > 在所有时区中使用 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);
}

标签: javascriptdate-conversion

解决方案


使用以下函数,我们可以将所有时区的 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));
        }

推荐阅读