csv 文件到 json 保持日期格式不变

问题描述 投票:0回答:0

我想将 CSV 文件转换为包含相同日期格式的 JSON 数据。

// this is the content of csv file
id,name,date,newdate
1,John,2022-01-01,01/02/2022
2,Mary,2022-02-15,02/02/2022

我想要相同的日期和新日期列而不更改日期格式。

fileReader.onload = (e) => {
   const csvData = e.target.result;
   const workbook = XLSX.read(csvData, { type: 'string' });
   const sheetName = workbook.SheetNames[0];
   const options = { header: 1, raw: true }; // specify the raw option here
   const data = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], options);
   console.log('data', data);
}

当前实施。结果是

[
    [
        "id",
        "name",
        "date",
        "newdate"
    ],
    [
        1,
        "John",
        44562.22928240741,
        44563.00011574074
    ],
    [
        2,
        "Mary",
        44607.22928240741,
        44594.00011574074
    ],

]

为什么日期列要转换成数字?我想要相同的格式字符串。 我使用 xlsx 库 import * as XLSX from 'xlsx';

javascript xlsx
© www.soinside.com 2019 - 2024. All rights reserved.