Date in XLS sheet not parsing correctly

So I got it working yesterday:

import * as XLSX from 'xlsx'
import * as df from 'dateformat';
// Typescript but easy to convert
const workbook: any = XLSX.read(source, { 'type': type, cellDates: true });
// passing the option 'cellDates': true is important
// ...
// ... until you start reading cells
const cellAddress: any = { c: 5, r: 5 }; // column 5 / row 5 which is a date and formatted like that in xlsx
const cell_ref: string = XLSX.utils.encode_cell(cellAddress);
const cell: any = worksheet[cell_ref];
if (cell) {
    let value: any = cell.v;
    const parsedDate: Date = new Date(value);
    parsedDate.setHours(parsedDate.getHours() + timezoneOffset); // utc-dates
    value = df(parsedDate, "dd/mm/yyyy");
}

This is just a snippet but the essentials are in there :-) Hope this might help you! Btw I am using a xlsx-file.

The trick is to pass the option 'cellDates: true' and then to just instantiate a date from the numbers value and it should work out. Your date will probably be a few hours off because of the utc-dates in JS, which you can even out by adding the offset to the date.


 const workbook = excel.readFile(file.path);

 const sheet_name_list = workbook.SheetNames;
 const json = excel.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]], {
  raw: false,
 });

var workbook = XLSX.read(data, {
  type: 'binary',
  cellDates: true,
  cellNF: false,
  cellText: false
});

Tags:

Xlsx

Node.Js