I have the following javascript code that convert date (string) to the Date Serial Number used in Microsoft Excel:
function JSDateToExcelDate(inDate) {
var returnDateTime = 25569.0 + ((inDate.getTime() - (inDate.getTimezoneOffset() * 60 * 1000)) / (1000 * 60 * 60 * 24));
return returnDateTime.toString().substr(0,5);
}
So, how do I do the reverse? (Meaning that a Javascript code that convert the Date Serial Number used in Microsoft Excel to a date string?
So, there I was, having the same problem, then some solutions bumped up but started to have troubles with the Locale, Time Zones, etc, but in the end was able to add the precision needed
The function's 'time' argument chooses between displaying the entire date or just the date's time
It's an old thread but hopefully I can save you the time I used readying around to write this npm package:
$ npm install
js-excel-date-convertPackage Usage:
You can verify these results with the example at https://docs.microsoft.com/en-us/office/troubleshoot/excel/1900-and-1904-date-system
The Code:
If you want to know how this works check: https://bettersolutions.com/excel/dates-times/1904-date-system.htm
No need to do any math to get it down to one line.
I'm in PST which is UTC-0700 so I used
offsetUTC = -17
to get 00:00 as the time (24 - 7 = 17).This is also useful if you are reading dates out of Google Sheets in serial format. The documentation suggests that the serial can have a decimal to express part of a day:
So, if you want to support a serial number with a decimal, you'd need to separate it out.
Although I stumbled onto this discussion years after it began, I may have a simpler solution to the original question -- fwiw, here is the way I ended up doing the conversion from Excel "days since 1899-12-30" to the JS Date I needed:
Essentially, it just builds a new Date object that is calculated by adding the # of Excel days (1-based), and then adjusting the minutes by the negative local timezone offset.
Try this:
Custom made for you :)