javascript 我正在转换excel工作表到JSON对象,而转换日期从excel是给我一些随机数,如何转换它到日期在JSON中

t2a7ltrp  于 2023-02-15  发布在  Java
关注(0)|答案(3)|浏览(133)

在excel中,我给出的日期为22/03/2019,转换为json,如下所示。
"receivedDate": 43546
我怎样才能得到原始格式?

var workbook = XLSX.read(bstr, { type: "binary" });
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];

resultJson = XLSX.utils.sheet_to_json(worksheet, { raw: true });

resultJson包含输出数据。我需要输出为22/03/2019

rlcwz9us

rlcwz9us1#

我遇到了同样的问题,请尝试执行以下操作。
将工作簿变量从:

var workbook = XLSX.read(bstr, { type: "binary" });

致:

var workbook = XLSX.read(bstr, {
                type: "binary", cellDates: true, dateNF: 'mm/dd/yyyy;@'
            });

我已经给出了一个样本日期格式,根据需要更改格式。
谢谢。

piztneat

piztneat2#

var workbook = XLSX.read(bstr, { type: "binary" });
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];

resultJson = XLSX.utils.sheet_to_json(worksheet, {dateNF:'mm/dd/yyyy;@',cellDates:true, raw: true});
oxiaedzo

oxiaedzo3#

I was facing this issue while working with Javascript using playwright
npm install -s excel-date-to-js

const { getJsDateFromExcel } = require("excel-date-to-js");

async ExcelDateToJSDate(formatDate) {
        const convDate = getJsDateFromExcel('42510');
       //output will be  2016-05-20T00:00:00.000Z
      // now covert it to get date 20/05/2016
        const inputDate = new Date(convDate);
        const updatedDate = inputDate.toLocaleDateString('en-GB');
        return updatedDate;

     }

相关问题