方法一:
(这种方法闰年转化后日期会有问题)
function formatDate(numb, format) { if (numb != undefined) { let time = new Date((numb - 1) * 24 * 3600000 + 1) time.setYear(time.getFullYear() - 70) let year = time.getFullYear() + '' let month = time.getMonth() + 1 + '' let date = time.getDate() + '' if (format && format.length === 1) { return year + format + month + format + date } return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date) } else { return undefined; } }
方法二:
(正确方法)
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; }