I am trying to apply the filter based on date in aggregation pipeline of mongo. See the aggregation pipeline below :
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
}
}
];
Now, the fromDate and toDate I am getting from below logic. Actually, I am trying to get the data between 1st and 12th of the current month from recharge_date field:
const date = new Date();
var fromDate = new Date(`${date.getFullYear()}-${date.getMonth()+1}-${01}`);
var toDate = new Date(`${date.getFullYear()}-${date.getMonth()+1}-${12}`);
But, When I am printing the fromDate and toDate in console, it is showing the below output :
2023-02-21T18:30:00.000Z
2023-03-02T18:30:00.000Z
I am not able to figure out how to remove this 18:30:00.000 time because of which I am getting wrong date as it is going 5 hours 30 minutes back of the time which I have defined in fromDate and toDate. Timezone is playing some role here.
I tried to use setHours(0,0,0,0) also but no success.
Thanks in advance for any response.
3
Answers
This will print :
setUTCHours() method is what we need to use.
Edit
Please see RobG’s answer for an explanation on why the behavior described by OP occurs and how you can use padding to get the expected result.
While my answer will resolve the issue, due to not parsing the actual
string
but instead providing year, month and day asnumber
s, it does not cover reasons on why parsing thestring
does not yield the expected result and why the result might differ on different browsers/ implementations of the ECMA standard.Orginal answer (please see edit!)
The difference results from the date time offset of your timezone. You need to create the date using
Date.UTC()
in order to get the time in UTC.The following will demonstrate the difference:
The issue is that in the code:
single digit months and days aren’t being padded, so you end up with a string in Feb 2023 like "2023-2-1" (note that the number
01
in${01}
will be stringified as"1"
). That isn’t consistent with the format supported by ECMAScript, so parsing is implementation dependent and may be treated as UTC, local or invalid.In the OP’s case, it’s being treated as local. In Safari, it’s treated as invalid.
If single digit months a days are padded, it will parse as UTC: