javascript - Node csv-stringify format timestamp column - Stack Overflow

I'm converting a list of objects into a csv using Node csv-stringify package.One of the columns co

I'm converting a list of objects into a csv using Node csv-stringify package.

One of the columns contains a time-stamp and the stringify method is converting this to an epoch date.

var stringify = require('csv-stringify');

...

input = [
{'field1':'val1', 'timemodified':'2016-08-16T23:00:00.000Z'},
...
]

stringify(input, function(err, output){
console.log(output);
})

The timemodified in output is formatted as:

1471388400000

How can I maintain the original time stamp formatting in the output?

I tried using the formatters option but it had no effect: /

 stringify(input, {formatters: {
      "timemodified": function(value){
        return value.format("YYYY/MM/DD hh:mm:ss");
      }
    }},function(err, output) {
      fs.writeFile('userUpload.csv', output, 'utf8', function(err) {
        if (err) {
          console.log('Error - file either not saved or corrupted file saved.');
        } else {
          console.log('userUpload.csv file saved!');
        }
      });
    });

I'm converting a list of objects into a csv using Node csv-stringify package.

One of the columns contains a time-stamp and the stringify method is converting this to an epoch date.

var stringify = require('csv-stringify');

...

input = [
{'field1':'val1', 'timemodified':'2016-08-16T23:00:00.000Z'},
...
]

stringify(input, function(err, output){
console.log(output);
})

The timemodified in output is formatted as:

1471388400000

How can I maintain the original time stamp formatting in the output?

I tried using the formatters option but it had no effect: http://csv.adaltas./stringify/examples/

 stringify(input, {formatters: {
      "timemodified": function(value){
        return value.format("YYYY/MM/DD hh:mm:ss");
      }
    }},function(err, output) {
      fs.writeFile('userUpload.csv', output, 'utf8', function(err) {
        if (err) {
          console.log('Error - file either not saved or corrupted file saved.');
        } else {
          console.log('userUpload.csv file saved!');
        }
      });
    });
Share Improve this question asked Jul 25, 2017 at 11:32 BchadwickBchadwick 3671 gold badge5 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

From the docs, you can pass "cast" to the options.

Example:

const stringify = require('csv-stringify');
const assert = require('assert');

stringify([{
  name: 'foo',
  date: new Date(1970, 0)
},{
  name: 'bar',
  date: new Date(1971, 0)
}],{
  cast: {
    date: function (value) {
      return value.toISOString()
    }
  }
}, function (err, data) {
  assert.equal(
    data,
    "foo,1969-12-31T23:00:00.000Z\n" +
    "bar,1970-12-31T23:00:00.000Z\n"
  )
})

I tried your code without the custom formatter and it works as expected. I get 'val1,2016-08-16T23:00:00.000Z' as output.

But I get the timestamp if I have a date object in my input like:

input = [
{'field1':'val1', 'timemodified': new Date('2016-08-16T23:00:00.000Z')},
...
]

Please check if there are date objects in your input.

You can find answer in doc examples

var moment = require('moment');
...
var stringifier = stringify(input, {
  formatters: {
    date: function(value) {
      return moment(value).format('YYYY-MM-DD');
    }
  }
}, function(err, output) {
  console.log(output);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744271678a4566132.html

相关推荐

  • javascript - Node csv-stringify format timestamp column - Stack Overflow

    I'm converting a list of objects into a csv using Node csv-stringify package.One of the columns co

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信