Here is my problem:
I have a grid that displays a list of records along with the date of the record. With that in mind I created a filter form that will allow the user to filter the data based on the dates. The problem here is how can I filter the dates. Here is what I have so far:
MyGrid is populated from my datastore:
recordsStore: new dojo.data.ItemFileWriteStore({
url: "xhreq?action=getRecords",
clearOnClose: true,
urlPreventCache: true
});
Inside my tablelayout I am using the dateTextBox For the user to add a date to filter on:
{
name : "Date",
field : "Date",
width : "10em",
type : dojox.grid.cells.DateTextBox,
formatter : formatDateObject,
constraint : {
datePattern : 'MM/dd/yyyy',
formatLength : 'short',
selector : 'date'},
editable: true
},
this is how I started working on filtering on my dates:
filter data from grid:
doFilter: function()
{
filter_standby.show();
inDate = "*";
if(filter_inDateFrom.attr("value") != "" ||
filter_inDateThrough.attr("value") != ""){
{
totalDays = inDateThru - inDateFrom; store totalDays
date = date + totalDays; // filter based on total days
}
Now is there a way to return the dates within a range? if not what is the best way to handle the dates?
Thanks
Related
I am creating a simple calendar with appointments.
I have documents with the date ranges example below.
{
"start_date" : "2020-02-06T19:58:25.430Z",
"end_date" : "2020-02-16T19:58:25.430Z"
},
{
"start_date" : "2020-02-17T19:58:25.430Z",
"end_date" : "2020-02-27T19:58:25.430Z"
},
{
"start_date" : "2020-03-21T19:58:25.430Z",
"end_date" : "2020-03-31T19:58:25.430Z"
},
If user entered start_date and end_date that has conflict on already created date range, system should prevent the user to insert new document.
How can I do it? can I use Moment JS?
You can create a unique compound index:
db.collection.createIndex({'start_date':1, 'end_date': 1}, {'unique': true})
I am sending the startDate and endDate in the URL and hits the query to find the data between startDate and endDate
var startDate = res.req.query.startDate ? moment(res.req.query.startDate, 'YYYY-MM-DD').valueOf() : null
var endDate = res.req.query.endDate ? moment(res.req.query.endDate, 'YYYY-MM-DD').valueOf() : null
if (startDate && endDate) {
query.dispatchDate = { $gte:startDate , $lte: endDate }
}
You did not clearly asked your question but i am suggestion you a solution what i understand.
In order to find data between 2 dates first you must add a field in db to track when the record is entered let suppose you have a collection named items and field to track when data is enter is created_date then you can find data between 2 dates like
items.find({
created_at: {
$gte: ISODate("2019-01-21T00:00:00.000Z"),
$lt: ISODate("2019-01-28T00:00:00.000Z")
}
})
for more details shHow to find objects between 2 dates in mongodb
I want to fetch the records from the CloudTable with particular date range say from : startDate to endDate
We can query all columns including DateTime type in a CloudTable. There are several condition you can add to your query like lessThan, greaterThan etc.
In this case, you can use the following query -
var query = new CB.CloudQuery("TableName");
query.lessThan('dateColumns',"15-12-2016");
query.greaterThan('dateColumns',"15-06-2017");
query.find({
success: function(list) {
//list is an array of CloudObjects
},
error: function(error) {
}
});
Hope it helps.Happy Coding :)
In netsuite we have months (Not in serial order).
months values are say :
list_month = [182,183,186,187,188,190,191,192,194,195,196,199,200,201];
I want to apply search filter to get records within certain months say from 183 to 194.
var period = 183;
var period1 = 194;
I used the "between" and "within" but it did not work'
Here is my filter:
filters.push(new nlobjSearchFilter("postingperiod","transaction","within",period,period1));
this returns only the values of 183. I want all of them : 183,186,187,188,190,191,192,194.
*Note this are not dates but months (starting from 1 to last date of that month)
How can i get this.
Thanks
You would need to specify each period as a separate filter and use .setParens(1) and .setOr(true) to build out the logic of your search like this:
var results = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('mainline', null, 'is', 'T'),
new nlobjSearchFilter('postingperiod', null, 'within', 122).setLeftParens(1).setOr(true),
new nlobjSearchFilter('postingperiod', null, 'within', 123).setRightParens(1)
], [
new nlobjSearchColumn('internalid', null, 'count')
]);
If you don't always know which periods you'll need, you can generate these filters dynamically with a function like this:
function buildPeriodFilters(periodIds) {
// Return empty array if nothing is passed in so our search doesn't break
if (!periodIds) {
return [];
}
// convert to array if only a single period id is passed in.
periodIds = [].concat(periodIds);
return periodIds.map(function(periodId, index, periodIds) {
var filter = new nlobjSearchFilter('postingperiod', null, 'within', periodId);
// if this is the first periodid, add a left parenthesis
if (index === 0) {
filter = filter.setLeftParens(1);
}
// if this is the last period id, add a right parenthesis, otherwise add an 'or' condition
if (index !== periodIds.length - 1) {
filter = filter.setOr(true);
} else {
filter = filter.setRightParens(1);
}
return filter;
});
}
var dynamicPeriodFilter = buildPeriodFilters([122,123,124]);
var results = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('mainline', null, 'is', 'T'),
].concat(dynamicPeriodFilter), [
new nlobjSearchColumn('internalid', null, 'count')
]);
I have a mongoDB collection and an item in the collection looks like below:
{
"_id": "52f535b56268a019bd11cc2a",
"description": "Some description",
"entry_date": "2014-02-07T19:36:21.430Z",
"last_update": "2014-02-07T19:36:21.430Z",
"r": "samestring",
"s": "samestring"
}
Dates are ISODate objects.
This query returns items correctly
db.myCollection.find({$where : "this.entry_date < this.last_update"});
Below query returns nothing (I expect that it returns the above item):
db.myCollection.find({$where : "this.entry_date == this.last_update"});
And this query returns all items (I expected again it returns the above item):
db.myCollection.find({$where :"this.r == this.s"});
What am I doing wrong?
Thanks!!
---EDIT----
So I tried to test with a small data like below:
> db.myCollection.find({},{ _id: 0, start_date: 1, end_date: 1});
{
"start_date" : ISODate("2014-02-07T19:36:21.430Z"),
"end_date" : ISODate("2014-02-07T19:36:21.430Z")
}
{
"start_date" : ISODate("2014-02-07T19:36:21.430Z"),
"end_date" : ISODate("2014-02-07T22:39:02.114Z")
}
It didn't work for Date as you can see:
> db.myCollection.find(
{$where: "Date(this.start_date) == Date(this.end_date)"},
{ _id: 0, start_date: 1, end_date: 1 }
);
{
"start_date" : ISODate("2014-02-07T19:36:21.430Z"),
"end_date" : ISODate("2014-02-07T19:36:21.430Z")
}
{
"start_date" : ISODate("2014-02-07T19:36:21.430Z"),
"end_date" : ISODate("2014-02-07T22:39:02.114Z")
}
Works for string values:
> db.myCollection.find({$where: "this.title == this.description"},{ _id: 0, title: 1 });
{ "title" : "samedescription" }
You have to be really careful when comparing dates in JavaScript - use valueOf() or getTime():
> db.myCollection.find({$where: "this.start_date.getTime() == this.end_date.getTime()"});
{ "_id" : ObjectId("52f5b7e316d795f0a076fbdf"), "description" : "a description", "title" : "a title", "start_date" : ISODate("2014-02-07T19:36:21.430Z"), "end_date" : ISODate("2014-02-07T19:36:21.430Z") }
Here is why your other queries didn't work.
db.myCollection.find({$where: "Date(this.start_date) == Date(this.end_date)"});
This didn't work because you didn't use new when initializing the dates. This generally has hilarious results with all dates being equal to each other:
> Date(2014,2,8) == Date(1941,12,7)
true
> Date(2000,1,1) == Date(1995,2,8)
true
But even if you properly instantiate the date using new, you still get hilarious results when comparing dates using ==, as demonstrated in this gist:
var dateValue = 504001800000; // Saturday, December 21st, 1985 at 3:30am EST
var date1 = new Date(dateValue);
var date2 = new Date(dateValue);
console.log(date1 == date2); // false (different instances)
console.log(date1 === date2); // false (different instances)
console.log(date1 > date2); // false (date1 is not later than date2)
console.log(date1 < date2); // false (date1 is not earlier than date2)
console.log(date1 >= date2); // true (rofl)
console.log(date1 <= date2); // true (ahahahaha)
As for your other query:
It didn't work if I consider them as strings either:
db.myCollection.find({$where: "this.start_date == this.end_date"});
You're not actually comparing them as strings, you're comparing ISODate objects, which is how they're stored. For ISODate, similar to Date, the == operator will return false unless you're comparing the exact same instance. Using getTime should work, however, which is what I did up above.
Hopefully, none of this makes any sense whatsoever, because if it does, I'm worried about your sanity.
--EDITED--
You were looking for the $where operator. Your query must be in a valid JSON notation and outside of this operator there is no other access to that kind of raw JavaScript notation.
Dates
finally:
{$where: "this.start_date.valueOf() == this.end_date.valueOf()"}
Also be careful not to run into reserved words and other traps.
Be very careful to read the documentation on this operator and make sure you absolutely need it. It will slow things down considerably as your find will scan the entire collection. It cannot use an index.