I'm using bootstrap datepicker.
I'm setting the minimum date to the current date and the max date to December of next year.
minDate = new Date();
minDate.setDate(1);
maxDate = new Date();
maxDate.setFullYear(maxDate.getFullYear() + 1);
maxDate.setMonth(12);
maxDate.setDate(0);
maxDate.setHours(23,59,59);
$('#startDateInput').datepicker({
format : "MM yyyy",
viewMode : "months",
minViewMode : "months",
startDate : minDate,
endDate : maxDate
});
$('#endDateInput').datepicker({
format : "MM yyyy",
viewMode : "months",
minViewMode : "months",
startDate : minDate,
endDate : maxDate
});
I'm reading the startdate and end date from the datepicker as below:
var startDateString = $('#startDateInput').val();
var startParts = startDateString.split(' ');
var startEpoch = new Date(parseInt(startParts[1]) , monthsArray.indexOf(startParts[0].toLowerCase()), 01).getTime();
document.getElementById('startDate').value = startEpoch;
var endDateString = $('#endDateInput').val();
var endParts = endDateString.split(' ');
var endEpoch = new Date(parseInt(endParts[1]) , monthsArray.indexOf(endParts[0].toLowerCase())+1,0,23,59,59).getTime();
I store the epochs in the database. When I retrieve the dates and show it in the edit form, I'm doing this
if ($('#object').val()) {
var stDate = new Date(Number("${object.startDate}"));
var enDate = new Date(Number("${object.endDate}"))
$('#startDateInput') .datepicker('setDate', new Date(stDate));
$('#endDateInput').datepicker('setDate', new Date(enDate));
}
Let's say I've selected dec 2019 as input today and saved it. Later when I edit the same form, the endDateInput is not filled because the end date is not inclusive, I suspect.
Related
I am using DatePicker of NativeBase and want to change the format of the displayed date after picking a date. I am unable to find a relevant prop due to lack of docs.
Is there a way I could change the format of the date as in DatePickerAndroid using format="YYYY-MM-DD"?
Fixed in native-base v2.6.1 onwards.
<DatePicker
formatChosenDate={date => {return moment(date).format('YYYY-MM-DD');}}
..... />
Try this:----
async onPressAction() {
try {
const {action, year, month, day} = await DatePickerAndroid.open({
// Use `new Date()` for current date.
// May 25 2020. Month 0 is January.
date: new Date(2018, 6, 26)
});
if (action !== DatePickerAndroid.dismissedAction) {
// Selected year, month (0-11), day
var date = new Date(year, month, day);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
console.log('>>>>>>>>>>>>>'+year);
console.log('>>>>>>>>>>>>>'+month);
console.log('>>>>>>>>>>>>>'+day);
console.log(year+'-'+month+'-'+day);
}
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
}
I want to compare date from MongoDB and my date.
Also i read this and this post and I did not find an answer.
My Code :
today: function() {
var today = moment().format();
return Posts.find({createdAt : { $gte : today}}) // show posts created in "future" , so this function must return nothing
},
createdAt = moment().format();// in MongoDB
As a result this construction doesn't work, but if i compare lie this :
var today = moment().format();
var daystart = moment().startOf('day').format();
if (daystart > today){
console.log ("YES");
}
else if (daystart < today)console.log ("NO");
Return
"NO"
Anybody help ?
EDIT :
today: function() {
var today = moment().toDate();
var daystart = moment().startOf('day').toDate();
// console.log(today + daystart);
return Posts.find({createdAt : { $gt : today}})
},
week: function() {
var today = new Date();
return Posts.find({createdAt : { $lt : today}})
},
month: function() {
var today = new Date();
return Posts.find({createdAt : { $ne : today}})
}
createdAt = new Date();
The .format() method is a display helper function which returns the date string representation based on the passed token argument. To compare the date from MongoDB with the the current date and time, just call moment() with no parameters, without the .format() method and get the native Date object that Moment.js wraps by calling the toDate() method:
today: function() {
var now = moment().toDate();
return Posts.find({createdAt : { $gte : now }});
}
Convert date to MongoDB ISODate format in JavaScript using Moment JS
MongoDB uses ISODate as their primary date type. If you want to insert a date object into a MongoDB collection, you can use the Date() shell method.
You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats:
new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.
new Date("<YYYY-mm-ddTHH:MM:ss>") specifies the datetime in the client’s local timezone and returns the ISODate with the specified datetime in UTC.
new Date("<YYYY-mm-ddTHH:MM:ssZ>") specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC.
new Date() specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance.
If you are writing code in JavaScript and if you want to pass a JavaScript date object and use it with MongoDB client, the first thing you do is convert JavaScript date to MongoDB date format (ISODate). Here’s how you do it.
var today = moment(new Date()).format('YYYY-MM-DD[T00:00:00.000Z]');
console.log("Next day -- " + (reqDate.getDate() + 1))
var d = new Date();
d.setDate(reqDate.getDate() + 1);
var tomorrow = moment(d).format('YYYY-MM-DD[T00:00:00.000Z]');
You can pass today and tomorrow object to MongoDB queries with new Date() shell method.
MongoClient.connect(con, function (err, db) {
if (err) throw err
db.collection('orders').find({ "order_id": store_id, "orderDate": {
"$gte": new Date(today), "$lt": new Date(tomorrow)}
}).toArray(function (err, result) {
console.log(result);
if (err) throw err
res.send(result);
})
})
I have used two date controls to filter a row repeater as,
oF_cell5 = new sap.ui.commons.layout.MatrixLayoutCell({id:"F05",colSpan : 2});
var oCreateFrom = new sap.ui.commons.DatePicker("EV_AE_DATE1",
{width:"150px",placeholder:"Created From",
change:function(oEvent){
oController.onChangeFilterValue(oEvent);}
})
oF_cell51 = new sap.ui.commons.layout.MatrixLayoutCell({id:"F051",colSpan : 2});
var oCreateTill = new sap.ui.commons.DatePicker("EV_AE_DATE2",
{width:"150px",placeholder:"Created Till",
change:function(oEvent){
oController.onChangeFilterValue(oEvent);}
});
Now i have a rowrepeater in which one of the column is CreatedOn date like..,,
new sap.m.HBox({
items:[new sap.ui.commons.TextView({text:"Created on:"}),
new sap.ui.commons.TextView("TV11")
.bindProperty("text",
{
path: "CM_EventList>CREATEDON",
type: new sap.ui.model.type.Date({pattern:"MMM dd, yyyy",
source : {pattern : "dd.MM.yyyy"}})
})]
}),
And in the controller i have written this code as....,,
onInit: function() {
var model = new sap.ui.model.json.JSONModel("eventlist.json");
model.setData();
sap.ui.getCore().setModel(model,"CM_EventList");
},
onChangeCmFilterValue : function(oEvent){
var CM_FDATEVAL = sap.ui.getCore().byId("EV_AE_DATE1").getValue();
var CM_TDATEVAL = sap.ui.getCore().byId("EV_AE_DATE2").getValue();
var CM_Date = new sap.ui.model.Filter('CM_EventList>CREATEDON',
sap.ui.model.FilterOperator.BT,CM_FDATEVAL,CM_TDATEVAL);
var oCM_VBOX1 = sap.ui.getCore().byId("EV_CM_VBOX");
var oCM_RR1 = sap.ui.getCore().byId("EV_AE_ROWREPEATER");
oCM_RR1.bindRows('CM_EventList>/eventlist',oCM_VBOX1,null,[CM_Date]);
},
And the eventlist is my seperate json file which has date values as
{
"eventlist": [
{
"CREATEDON": "10.07.2014",
},
{
"CREATEDON": "10.08.2014",
},
.......
and so on..........
Now if select a date range from my date controls then the row repeater should show the records which are between the range of dates as in my json.
But the filter is not working.
Please Help me on this.
Thanks
Sathish
First of all, use the DatePicker Control for date fields in your view if you aren't using it already.
You can obtain the value of your date picker as a Date object using the method GetDateValue(). You can then use these date objects to create a filter for a datetime field of your data model.
var dateFrom = this.getView().byId("filterDateFrom").getDateValue();
var dateTo = this.getView().byId("filterDateTo").getDateValue();
if (dateFrom != null && dateTo != null) {
filter = new sap.ui.model.Filter(
"CM_EventList>CREATEDON",
sap.ui.model.FilterOperator.BT,
dateFrom,
dateTo
);
}
By the way: Note that both date objects will actually represent the moment at the beginning of the day (0:00:00) while the timestamps in your database will often be some point in time throughout the day. So when you want to search between two dates inclusively, you need to add one day to dateTo:
dateTo.setDate(dateTo.getDate() + 1);
Another problem you might or might not have to deal with are timezones... and of course all the other falsehoods programmers believe about time.
I think you should check the value of the following. The format should be different than your json value "CREATEDON": "10.08.2014".
var CM_FDATEVAL = sap.ui.getCore().byId("EV_AE_DATE1").getValue();
var CM_TDATEVAL = sap.ui.getCore().byId("EV_AE_DATE2").getValue();
Please try create a DatePicker with:
type: new sap.ui.model.type.Date({pattern: ""yyyy-MM-dd""})
Edit: to use Date as filter
var CM_FDATEVAL_DATE = new Date(sap.ui.getCore().byId("EV_AE_DATE1").getValue());
var CM_TDATEVAL_DATE = new Date(sap.ui.getCore().byId("EV_AE_DATE2").getValue());
Regards,
Allen
I need to pull from the DB all rows in a day
var i_sDate = "2014-06-21"; // (user input)
var startDate = new Date();
var month = parseInt(i_sDate.substr(5,2)) - 1;
var day = i_sDate.substr(8,2);
startDate.setFullYear(i_sDate.substr(0,4), month, day);
startDate.setHours(0, 0, 0, 0);
var endDate = new Date();
endDate.setFullYear(i_sDate.substr(0,4), month, day);
endDate.setHours(23, 59, 59, 0);
var query = {start_time:{"$gte": "ISODate('" + startDate.toISOString() + "')", "$lt": "ISODate('" + endDate.toISOString() + "')"}};
var tableInfo = Users_Collection.find(query).fetch();
console.log(query);
when I print "query" it looks OK, but I don't get any result at all, I put the same information directly on the DB and I get the expected result. It seems like I'm building the query in the wrong way, any suggestion?????
thanks in advance!
You should directly use Date objects in your query. Try this:
var query = {start_time: {$gte: startDate, $lt: endDate}};
It also looks like you were missing a closing }.
I have a JSON model which contains, among others, a few date values which are stored as epoch values:
var oData = [{
string : "SomeValue",
date : 1404172800000
}];
When I load my model, I convert this epoch to a proper Javascript Date object using:
for (var i = 0; i < oData .length; i++) {
var dateLong = oData[i].date;
oData[i].date = new Date(dateLong);
}
In my table, I then render the column using a formatter function:
var oDateColumn = new sap.ui.table.Column({
label: new sap.ui.commons.Label({
text: "A Date"
}),
template: (new sap.ui.commons.TextView({
text : {
parts : [date],
formatter : function(oValue) {
if (oValue != undefined) {
var yyyy = oValue.getFullYear().toString();
var mm = (oValue.getMonth()+1).toString(); // getMonth() is zero-based
var dd = oValue.getDate().toString();
return yyyy + '/' + (mm[1]?mm:"0"+mm[0]) + '/' + (dd[1]?dd:"0"+dd[0]);
} else return "";
}
},
textAlign : sap.ui.core.TextAlign.Right
})),
sortProperty : "date",
filterProperty : "date",
filterOperator : sap.ui.model.FilterOperator.EQ
});
This works ok, and the former epoch is now a date which is nicely rendered as '2014/07/01'
However, the filtering is not on the formatted date but on the original Date object -- if I filter on '2014/07/01' I get no results; if I filter on '1404172800000' I get the filtered results...
I tried using a formatter on the filterProperty but I wasn't able to get this to work.
Does anyone know how I can have users filter on the formatted date?
Using a date type might solve this issue
var dateType = new sap.ui.model.type.Date({
pattern: "yyyy/MM/dd"
});
...
sortProperty : "date",
filterProperty : "date",
filterType: dateType
look at this example for a simple use case on birthday column