javascriptdatexlsxjs-xlsx

Javascript convert short date number to javascript date


I have a .xlsx file which has a table which contains dates. When I read the table in to javascript (with xlsx nodejs package) the dates are formatted as numbers. I would like to convert these numbers to dates. For example the number 43527 should be the date 13/03/2019. I have tried new Date(43527*60*60*24*1000) but this gives the wrong answer of 2089-03-04T00:00:00.000Z.


Solution

  • Excel stores dates internally using OLE Automation Date (OADate).

    The base OLE Automation Date is midnight, 30 December 1899. OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. [https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tooadate?view=netcore-3.1#remarks]

    You need to subtract 25,569, which is the number of days between 30 December 1899 and 1 January 1970, before converting to milliseconds.

    To convert the date:

    var excelDate = 43527;
    var parsedDate = new Date((excelDate - 25569) * 86400 * 1000)).toISOString();
    

    As mentioned here Converting Excel Date Serial Number to Date using Javascript