I've a usecase in which I need to find the data of a particular month. How to get the start and end dates of given month?
Here's the sample code.
{"_id":"5e00bc55c31ecc38d023b156","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T13:08:37.841Z"},
{"_id":"5e00bbd2c31ecc38d023b155","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T13:06:26.366Z"},
{"_id":"5df4a8fb46b9da1e2c0731df","heat":88,"humidity":80,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-14T09:18:51.892Z"},
{"_id":"5e00b50bc127260398cf51dd","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T12:37:31.127Z"},
{"_id":"5df20e44e7c51b4bd0095af3","heat":41,"humidity":26,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-12T09:54:12.375Z"}
Here's my code without moment.js
Payload:
{
"deviceId":"a-1",
"year":2019,
"month":"December"
}
Collection.aggregate([
{
$match: {
"deviceId": payload.deviceId,
"entryDayTime": {
$lt: new Date(`${payload.month},${payload.year},2`).toISOString(),
$gte: new Date(`${payload.month},${payload.year},31`).toISOString()
}
}
}
])
These are the time ranges I'm getting in console(times passed in aggregate function),
2019-12-01T18:30:00.000Z
2019-12-30T18:30:00.000Z
Code with moment.js
Payload:
{
"deviceId":"a-1",
"year":2019,
"month":10
}
I've tried with moment.js too. But I'm not getting the times in the format like time format of database.
Collection.aggregate([
{
$match: {
"deviceId": payload.deviceId,
"entryDayTime": {
$lt:moment([payload.year]).month(payload.month).startOf('month').tz('Asia/Kolkata').format(),
$gte:moment([payload.year]).month(payload.month).endOf('month').tz('Asia/Kolkata').format()
}
}
}
])
Following are the timestamps I'm getting in console.
2019-11-01T00:00:00+05:30
2019-11-30T23:59:59+05:30
If moment.js is preferred, how to change the time format similar to the sample code's time format?
Just try this code:
var dated="2019-11-01T00:00:00+05:30";
var newdated= new Date(dated);
var output= newdated.toISOString();
console.log(output);
Result :
'2019-10-31T18:30:00.000Z'
The toISOString()
method returns a string in simplified extended ISO format (ISO 8601)
, which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ
or ±YYYYYY-MM-DDTHH:mm:ss.sssZ
, respectively).
The timezone is always zero UTC offset, as denoted by the suffix "Z".
To find the data of a particular month use Date.UTC
with the Date
constructor to create a range:
const payload = {
"deviceId": "a-1",
"year": 2019,
"month": 11 // months start from 0 = January, so 11 = December
}
const from = new Date(Date.UTC(payload.year, payload.month, 1)).toISOString(); // "2019-12-01T00:00:00.000Z"
const to = new Date(Date.UTC(payload.year, payload.month + 1, 1)).toISOString(); // "2020-01-01T00:00:00.000Z"
then use them as follows:
Collection.aggregate([
{
$match: {
"deviceId": payload.deviceId,
"entryDayTime": {
$lt: to,
$gte: from
}
}
}
])
Working example : https://mongoplayground.net/p/jkIJdJ-L7q-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.