js解析excel数据时间格式转换问题

导入excel的日期是 44081.73594907407 这种格式的

 function formatDate(serial, format) {
        var utc_days = Math.floor(serial - 25569);
        var utc_value = utc_days * 86400;
        var date_info = new Date(utc_value * 1000);
        var returnDate = date_info.getFullYear();
        returnDate += format + (date_info.getMonth() < 9 ? '0' + (date_info.getMonth() + 1) : (date_info.getMonth() + 1));
        returnDate += format + (date_info.getDate() < 10 ? '0' + date_info.getDate() : date_info.getDate());
        returnDate += " " + (date_info.getHours() < 10 ? '0' + date_info.getHours() : date_info.getHours());
        returnDate += ":" + (date_info.getMinutes() < 10 ? '0' + date_info.getMinutes() : date_info.getMinutes());
        console.log(date_info)
        return returnDate;
    }  

通过以上代码解析年月日时分秒是有问题的

找了好久 通过如下代码可以准确的解析

 function formatDate(serial, format) {
        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;

        var daytime = new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate(), hours, minutes, seconds);
        var returnDate = date_info.getFullYear();
        returnDate += format + (date_info.getMonth() < 9 ? '0' + (date_info.getMonth() + 1) : (date_info.getMonth() + 1));
        returnDate += format + (date_info.getDate() < 10 ? '0' + date_info.getDate() : date_info.getDate());
        returnDate += " " + (daytime.getHours() < 10 ? '0' + daytime.getHours() : daytime.getHours());
        returnDate += ":" + (daytime.getMinutes() < 10 ? '0' + daytime.getMinutes() : daytime.getMinutes());
        return returnDate;
    }  

希望可以给帮助到到部分开发者

你可能感兴趣的:(h5,js,javascript,html)