Skip to content

Commit 0e821ea

Browse files
authored
Added useDateIso8601Format (#169)
* Update json2csv.js added useDateIso960Format * Update converter.d.ts added useDateIso8601Format * Update json2csv.js tweaked to avoid calling isDate twice
1 parent c51ba95 commit 0e821ea

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/converter.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export interface IFullOptions extends ISharedOptions {
8282
*/
8383
useLocaleFormat?: boolean;
8484

85+
/**
86+
* Should dates be output in ISO 8601 "Z" format:
87+
* @default false
88+
*/
89+
useDateIso8601Format?: boolean;
90+
8591
}
8692

8793
export function json2csv(data: object[],

src/json2csv.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,15 @@ const Json2Csv = function(options) {
272272
* @returns {*}
273273
*/
274274
function recordFieldValueToString(fieldValue) {
275-
if (Array.isArray(fieldValue) || utils.isObject(fieldValue) && !utils.isDate(fieldValue)) {
275+
const isD = utils.isDate(fieldValue); // store to avoid checking twice
276+
if (Array.isArray(fieldValue) || utils.isObject(fieldValue) && !isD) {
276277
return JSON.stringify(fieldValue);
277278
} else if (utils.isUndefined(fieldValue)) {
278279
return 'undefined';
279280
} else if (utils.isNull(fieldValue)) {
280281
return 'null';
282+
} else if (isD && options.useDateIso8601Format) {
283+
return new Date(fieldValue).toISOString();
281284
} else {
282285
return !options.useLocaleFormat ? fieldValue.toString() : fieldValue.toLocaleString();
283286
}

0 commit comments

Comments
 (0)