gpt4 book ai didi

javascript - Node.js 和 csv-express 日期格式

转载 作者:行者123 更新时间:2023-12-01 01:43:04 27 4
gpt4 key购买 nike

使用 csv-express 导出到 csv 时,有没有办法格式化日期“YYYY-MM-DD”?我已经用谷歌搜索了很长时间但什么也没找到。我正在使用最新版本的 Node.js、Express 和 MongoDB。

这是默认日期格式:“Thu Aug 09 2018 00:00:00 GMT-0700(太平洋夏令时间)”。我只想要这个“2018-09-09”。

这是我的查询:

router.get('/exportMonthlyPosts', (req, res) => {
posts.find({...}, {id:1, title:1, post:1, postedOn:1}).sort(orderBy).lean()
.then(post => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/csv');
res.setHeader("Content-Disposition", 'attachment; filename=posts.csv');
res.csv(report, true);

});
});

示例数据:

[
{
_id: 5b64c23eef5b9c5c60fa42a0,
title: 'Test Post',
post: 'Show me the money!',
postedOn: 2018-08-29T00:00:00.000Z
},
{
_id: 5afb58408341f161a0c96608,
title: 'Test Post 2',
post: 'Show me the money!',
postedOn: 2018-08-29T00:00:00.000Z
}
]

谢谢!

最佳答案

你试试这个吗? momentjs

这是很酷的模块。

https://momentjs.com/

你可以做到这一点。

router.get('/exportMonthlyPosts', (req, res) => {
posts.find({...}, {id:1, title:1, post:1, postedOn:1}).sort(orderBy).lean()
.then(post => {
// update postedOn.
report.forEach((r) => {
r.postedOn = moment(r.postedOn).format("YYYY-MM-DD")
});

res.statusCode = 200;
res.setHeader('Content-Type', 'text/csv');
res.setHeader("Content-Disposition", 'attachment; filename=posts.csv');
res.csv(report, true);

});
});

关于javascript - Node.js 和 csv-express 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com