How to query mongoDB for a given date range? - mongodb

I am using mongoose to query data for my node.js application for a given month where I accept the input for a year and month from the user.
This is what I am doing
var lastDay = 31
if (month == 3 || month == 5 || month == 8 || month == 10) lastDay = 30
else if (month == 1) lastDay = 28
var cookies = await Cookies.find({
docDate: {
"$gte": new Date(parseInt(year), parseInt(month), 1),
"$lte": new Date(parseInt(year), parseInt(month), lastDay)}}).sort("docDate");
When I try to query the db for the month of february, I am getting results only uptil the 26 Feb and the result also includes data from january.
Please help me figure out what I am doing wrong.

I think the issue is with the date that is passed to the query.
new Date(2021,01,01); --> 2021-01-31T16:00:00.000Z
new Date(Date.UTC(2021,01,01)); --> 2021-02-01T00:00:00.000Z
If the date field in the database is in UTC then try using new Date(Date.UTC(year, month, day))

Use moment.js library:
docDate: {
"$gte": moment().startOf('month').toDate(),
"$lte": moment().endOf('month').toDate()
}
moment() is the current time. In order to input specific day, use moment(year + '-' + month, "YYYY-MM") for example.

Related

Display Data between Dec Y-1 and Dec current Y in PowerBI

I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )

Add Days Google Script

I wondered if anyone could help. I have a script where I am pulling out data from a spreadsheet list, where this is a match for this week (basically an events list, to produce a weekly agenda). I will use a for loop to increment the days to add on, but I am just trying to make it work for one day for now...
The first column is the data in format dd/mm/yyy
I am trying to take today's increment by 1 and then search through the list to find a match. The searching etc, I can make work, but the date part is just not playing. I wondered if anyone could advise.
E.g. Date Column A:
06/07/2021
06/07/2021
01/11/2021
01/11/2021
01/11/2021
01/11/2021
02/09/2021
02/09/2021
var selectedDate = row[0];
selectedDate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var currdate = new Date();
currdate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var daystochange = 1;
var newdate = new Date(currdate.getFullYear, currdate.getMonth, currdate.getDay+daystochange );
Could anyone help?
Thanks
Only use Utilities.formatDate() to output dates, not to work with dates.
The JavaScript date object has all you need to work with dates and compare. When you use the Utilities function it converts it to a string, and so you lose all the functionality of the Date object.
Also bear in mind that if you have dates, that are formatted as dates in your sheet, they will automatically be returned as Date objects.
For example, if your sheet has a date in cell A1
var date = Sheet.getRange("A1").getValue()
date instanceof Date // true
Once you have your date, if you want to add one day to it, you can take an approach similar to what you have already done:
var selectedDate = new Date(2021, 1, 15)
var newdate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + 1);
console.log(newdate) // Tue Feb 02 2021 00:00:00
Note - use getDate to return the day of the month, getDay only returns day of the week.
To check if two dates are the same, you can write a function to compare:
function isSameDate(a, b) {
return a instanceof Date &&
b instanceof Date &&
a.getYear() === b.getYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
}
This function will return true if the dates are the same.
Reference
Date

getting Date months/ rest of days difference

is there a way to get these information from two dates:
calculate month between dates ( month for me is a complete month)
calculate rest of days between dates
here is my example:
start date:
01/01/2014
end date:
21/02/2014
i need a resualt like this : months:1 days:20
onother example:
start date:
15/01/2014
end date:
10/03/2014
i need a resualt like this : months:1 days:25
Using Java8 Date/Time API you may do it like so,
LocalDate startDate = LocalDate.of(2014, 1, 1);
LocalDate endDate = LocalDate.of(2014, 2, 21);
Period period = startDate.until(endDate);
System.out.println("months: " + period.getMonths() + " days: " + period.getDays());

Hoe to fetch records which is closed in 1 day in mongoose

*How i can get all record who's are closing in one day in mongodb. For example closing date 6th DEC. Can i check it with 5 DEC date in mongo query *
var start=new Date();
var dat = new Date();
dat.setDate(dat.getDate() + 7);
var end=dat;
var criteria = {
EndDate: {
$gte: start,
$lte: end
}
}
using this condition first i get all the records which are closed within a week from today date

Mongo DB ISO format

Based on the below query return result I want to filter the month and ther year.
For example I only want data for the month of March.
db.SBM_USER_DETAIL.aggregate([
{
$project: {
join_date: '$JOIN_DATE'
}
}
]).map(
function(d) {
d.join_date = moment(d.join_date).locale('es').tz("Asia/Kolkata").format();
return d
})
How to use the returned formatted value of join_date inside the MongoDB aggregation query?
MongoDB's ISODate is very similar to the javascript Date class. If you have a date range in the Kolkata timezone, and want to filter by that, instantiate a pair of Date objects to define the range, before running the find.
For this instance, to return all join_date values that fall within March 2017, converted to the Kolkata (UTC-07:00) timezone, filter for date greater than or equal to midnight March 1 and less than midnight April 1, then convert the results using moment:
var first = new Date("2017-03-01T00:00:00-07:00");
var last = new Date("2017-04-01T00:00:00-07:00");
db.SBM_USER_DETAIL.find(
{join_date:{$gte: first, $lt: last}}, //filter based on join_date
{join_date:1,_id:0} // only return join_date, omit this if you need all fields
).map(
function(d) {
d.join_date = moment(d.join_date).locale('es').tz("Asia/Kolkata").format();
return d;
}
);