使用Javascript将Exceldate序列号转换为date

我有以下JavaScript代码将date(string)转换为Microsoft Excel中使用的date序列号:

function JSDateToExcelDate(inDate) { var returnDateTime = 25569.0 + ((inDate.getTime() - (inDate.getTimezoneOffset() * 60 * 1000)) / (1000 * 60 * 60 * 24)); return returnDateTime.toString().substr(0,5); } 

那么,我该怎么做呢? (意思是将Microsoft Excel中使用的date序列号转换为datestring的Javascript代码?

尝试这个:

 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); } 

定制为你:)

我为你做了一个单行的

 function ExcelDateToJSDate(date) { return new Date(Math.round((date - 25569)*86400*1000)); } 
 // Parses an Excel Date ("serial") into a // corresponding javascript Date in UTC+0 timezone. // // Doesn't account for leap seconds. // Therefore is not 100% correct. // But will do, I guess, since we're // not doing rocket science here. // // https://www.pcworld.com/article/3063622/software/mastering-excel-date-time-serial-numbers-networkdays-datevalue-and-more.html // "If you need to calculate dates in your spreadsheets, // Excel uses its own unique system, which it calls Serial Numbers". // lib.parseExcelDate = function (excelSerialDate) { // "Excel serial date" is just // the count of days since `01/01/1900` // (seems that it may be even fractional). // // The count of days elapsed // since `01/01/1900` (Excel epoch) // till `01/01/1970` (Unix epoch). // Accounts for leap years // (19 of them, yielding 19 extra days). const daysBeforeUnixEpoch = 70 * 365 + 19; // An hour, approximately, because a minute // may be longer than 60 seconds, see "leap seconds". const hour = 60 * 60 * 1000; // "In the 1900 system, the serial number 1 represents January 1, 1900, 12:00:00 am // while the number 0 represents the fictitious date January 0, 1900". // These extra 12 hours are a hack to make things // a little bit less weird when rendering parsed dates. // Eg if a date `Jan 1st, 2017` gets parsed as // `Jan 1st, 2017, 00:00 UTC` then when displayed in the US // it would show up as `Dec 31st, 2016, 19:00 UTC-05` (Austin, Texas). // That would be weird for a website user. // Therefore this extra 12-hour padding is added // to compensate for the most weird cases like this // (doesn't solve all of them, but most of them). // And if you ask what about -12/+12 border then // the answer is people there are already accustomed // to the weird time behaviour when their neighbours // may have completely different date than they do. // // `Math.round()` rounds all time fractions // smaller than a millisecond (eg nanoseconds) // but it's unlikely that an Excel serial date // is gonna contain even seconds. // return new Date(Math.round((excelSerialDate - daysBeforeUnixEpoch) * 24 * hour) + 12 * hour); };