How to save datepicker date as mongodb date? - mongodb

In a meteor app I select a date via jquery datepicker, this is triggered by
click .tododateDue. After providing all information in my dialog all fields of the todo are saved via click .saveTodo
I like to display the date in my input field as dd.mm.yy but I need to save it in a mongodb collection as 'date'.
Since I use todo.datedue = tmpl.find('.tododateDue').value; to save the date I get a String in my collection.
How can I save this date as the type 'date' in the mongodb collection?
Template.todoDlg.events({
'click .saveTodo':function(evt,tmpl){
console.log('tmpl',tmpl);
var todo = {};
todo.note = tmpl.find('.todoitem').value;
todo.title = tmpl.find('.todotitle').value;
todo.datedue = tmpl.find('.tododateDue').value;
todo.project = Session.get('active_project');
Meteor.call('addTodo',todo);
Session.set('adding_todo',false);
},
'click .tododateDue': function (evt, tmpl) {
Meteor.setTimeout(function () {
$('.tododateDue').datepicker({
onSelect: function (dateText) {
console.log('date',tmpl.find('.tododateDue').value);
//Meteor.call('updateProjectDate', Session.get('active_project'), dateText);
},
dateFormat:'dd.mm.yy'
});
}, 100)
}
})

I think, you can use moment.js:
todo.datedue = moment(tmpl.find('.tododateDue').value, "dd.mm.yy").toDate();
It will return Date-object...

Perhaps autoform would help you here.
http://autoform.meteor.com/types

Related

Store and Retrieve Date in dd MMM yyyy format in MongoDB model

I have a MongoDB model that contains a Date field whose type is defined as Date.now. Any date is converted to ISO date format. Inside the model the date is defined as :
xDate : {
type: Date.now,
required: true
}
I pass the current Date as :
var d = new Date();
var temp = d.toISOString();
var subStr = temp.substr(10,temp.length - 1);
var curDate = temp.replace(subStr, "T00:00:00.000Z");
console.log(curDate);
However the date is stored as an ISO String inside the MongoDB schema. I try to query it using Mongoose using the following query:
X.
find({
xDate: curDate
})
.exec(function(err, doc) {
var response = {
status : 200,
message : doc
};
if (err) {
console.log('Error');
response.status = 500;
response.message = err;
} else if (!doc) {
console.log("Documents against the date not found in database" ,curDate);
response.status = 404;
response.message = {
"message" : "Documents not found for " + curDate
};
}
res
.status(response.status)
.json(response.message);
});
I keep getting a blank json array inspite of the data being there. Inside the table the xDate is stored as YYYY-MM-DD format.
The date inside mongo is not stores in ISO string. If you save your model as Date.now, it will save a new Date object, not an ISO string. So one easy way of querying is to query by new Date() object.
Also note that your query is hard to be true, since you will have a hard time getting the exactly same date as your data is stored. I think better option for you is using $lt or $gt filters.
New query should look something like:
let currDate = new Date()
// change the date using class methods
X.find({
xDate: {$lt: currDate}
}).exec...

Amazon DynamoDB: How to search for range of dates?

I have a DynamoDB and some items there have a date field. The date field is a string of the format {YYYY-MM-DD}. What should I have to write that the DB will retrieve all items which date field is between a start date and an end date?
This is my code:
function searchFile(from_date, until_date) {
AWS.config = new AWS.Config({accessKeyId: '***', secretAccessKey: '***', region: '***'});
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
"TableName" : '***',
FilterExpression: "Date_ = :date",
ExpressionAttributeValues: {
// What should I write here?
},
}
dynamodb.scan(params, function(err,data) {
if (err) {
console.log(err);
}
console.log(data);
})
}
The DynamoDB stores dates as String. You can use BETWEEN operator to get the range of dates.
createdate - is the attribute name
FilterExpression: "createdate BETWEEN :date1 and :date2",
ExpressionAttributeValues: {
":date1": "2010-05-05",
":date2": "2011-10-04",
}
Date S (string type). The Date values are stored as ISO-8601 formatted
strings.
BETWEEN : Greater than or equal to the first value, and less than or
equal to the second value.

Save date with DD/MM/YYYY

How can I save DD/MM/YYYY format into mongodb database in date type?
After saving it into mongodb, when I retrieved, how can I convert it back to DD/MM/YYYY?
I am using Mongoose.
Better way to store dates in mongodb is store them by using native javascript date object.
They allows you to use some useful methods (comparison, map reduce, ...) in mongodb natively.
Then, you can easily get formatted date by using mongoose virtuals, e.x.:
// describe your schema
var schema = new Schema({
time: Date
}, {
toObject: { getters: true }
});
// schema.formatted_time -> DD/MM/YYYY
schema.virtual('formatted_time').get(function() {
var date = new Date(this.time);
return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
});

Date Filter in sapui5

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

jqGrid filterToolbar - filter on DateTime using jquery ui datepicker

I have been looking at filtering jqGrid by datetime using the filterToolbar.
My question is based on Olegs excellent answer here.
I finally figured out how to trigger toolbar search on date as follows:
colModel: [{
name: 'RequestDate',
index: 'RequestDate',
formatter: 'date',
formatoptions: {
newformat: 'm/d/Y h:iA'
},
searchoptions: {
sopt: ['eq'],
dataInit: function (elem) {
$(elem).datepicker({
changeYear: true,
changeMonth: true,
onSelect: function (dateText, inst) {
setTimeout(function () {
$('#MyGrid')[0].triggerToolbar();
}, 50);
}
});
}
}
}]
Now when selecting the date from the picker I want to return all records for the given date ignoring the time.
I have tried updating the FilterObjectSet method with no luck. Has anyone been able to implement this successfully?
What I've tried: (see the code in Olegs linked solution)
Setting the FormatMapping to "(dateadd(dd,0, datediff(dd,0, it.{0})) = #p{1})" and
addingSystem.DateTime to the switch statement:
case "System.DateTime":
param = new ObjectParameter("p" + iParam, Convert.ToDateTime(rule.data));
break;
But this will result in a EntitySqlException:
'dateadd' cannot be resolved into a valid type or function.
Does anyone have a solution?
Ok figured it out this morning:
Added a new Operation:
de //de - date equals
Added a new string to FormatMapping that uses SqlServer.datediff:
"(SqlServer.datediff('DAY', it.{0} , #p{1}) = 0)" //de - date equals
and added the date case:
case "System.DateTime":
param = new ObjectParameter("p" + iParam, Convert.ToDateTime(rule.data));
break;
Changed sopt in colModel to sopt: ['de']