CSVParse for Node.js

IssuesGitHub

Option cast_date

The cast_date option convert the CSV field to a JavaScript date. It requires the cast option to be active.

The implementation relies on Date.parse. The CSV value is left untouched if the function returns NaN.

History

This option was named auto_parse_date until version 2.

Usage

When active cast_date with true, every field is tested with Date.parse. When the convertion to a JavaScript date succeed, the new date is returned. Otherwise, the original value is returned.

import assert from 'node:assert';
import { parse } from 'csv-parse/sync';

const data = `
2000-01-01,date1
2020-01-01,date2
`.trim();
const records = parse(data, {
  cast: true,
  cast_date: true
});
assert.deepStrictEqual(records, [
  [ new Date('2000-01-01T00:00:00.000Z'), 'date1' ],
  [ new Date('2020-01-01T00:00:00.000Z'), 'date2' ]
]);

About

The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.