Skip to content

Add dateUTC flag to CSV Writing #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,8 @@ The CSV parser uses [fast-csv](https://www.npmjs.com/package/fast-csv) to write

Dates are formatted using the npm module [moment](https://www.npmjs.com/package/moment).
If no dateFormat is supplied, moment.ISO_8601 is used.
When writing a CSV you can supply the boolean dateUTC as true to have ExcelJS parse the date without automatically
converting the timezone using `moment.utc()`.

### Streaming I/O

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ export interface CsvReadOptions {

export interface CsvWriteOptions {
dateFormat: string;
dateUTC: boolean;
}

export interface Csv {
Expand Down
3 changes: 2 additions & 1 deletion lib/csv/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ CSV.prototype = {
csvStream.pipe(stream);

var dateFormat = options.dateFormat;
var dateUTC = options.dateUTC;
var map = options.map || (value => {
if (value) {
if (value.text || value.hyperlink) {
Expand All @@ -123,7 +124,7 @@ CSV.prototype = {
return value.result || '';
}
if (value instanceof Date) {
return dateFormat ? moment(value).format(dateFormat) : moment(value).format();
return dateFormat ? dateUTC ? moment.utc(value).format(dateFormat) : moment(value).format(dateFormat) : moment(value).format();
}
if (value.error) {
return value.error;
Expand Down