我尝试在mongo的聚合管道中应用基于日期的过滤器。请参见下面的聚合管道:
var projectQry = [
{
$match: {
"prmid": userId
}
},
{
"$unwind": {
path : '$performanceData'
}
},
{
$match: {
performanceData.recharge_date": {
$gte: fromDate, $lte: toDate
}
}
},
{
$group : {
_id: "$performanceData.campaign_name",
basecount: {
$sum: "$performanceData.basecount"
}
}
},
{
$project: {
_id: 0,
campaign_name: "$_id",
basecount: 1
}
}
];
现在,我从下面的逻辑中获取fromDate和toDate,实际上,我试图从recharge_date字段中获取当月1日到12日之间的数据:
const date = new Date();
var fromDate = new Date(`${date.getFullYear()}-${date.getMonth()+1}-${01}`);
var toDate = new Date(`${date.getFullYear()}-${date.getMonth()+1}-${12}`);
但是,当我在控制台中打印fromDate和toDate时,它显示以下输出:
2023-02-21T18:30:00.000Z
2023-03-02T18:30:00.000Z
我不知道如何删除这个18:30:00.000时间,因为我得到了错误的日期,因为它是5小时30分钟后,我已经在fromDate和toDate中定义的时间。时区在这里发挥了一些作用。
我也尝试使用setHours(0,0,0,0),但没有成功。
提前感谢您的回复。
2条答案
按热度按时间u4dcyp6a1#
差异是由时区的日期时间偏移量造成的。您需要使用
Date.UTC()
创建日期,以获取UTC时间。以下内容将说明差异:
2q5ifsrm2#
这将打印:
setUTCHours()方法是我们需要使用的。