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
3 Answers
Reset to default 5From 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
评论列表(0条)