I am trying to get only date pate part from timestamp value using DATE() in MySQL its showing result correct in MySQL but in node js its showing full-time stamp instead of date.
Example : -
SELECT DATE("2020-04-12T18:30:00.000Z");
Generating 2020-04-12 in MySQL but in node js while fetching through my own API it's giving "2020-04-12T18:30:00.000Z" this.
Help me to Solve this.
You can use in the query SQL this:
SELECT ...,(DATE_FORMAT("2017-06-15", "%Y-%m-%d") as finalDate) FROM ...
And now in your API, when you call to MySQL with the previous query you should have 2017-06-15 in finalDate. The type DATE in MySQL is a Date Object, so if you want only date in string, you need to cast it in MySQL with the previous code or in your API with parsing date to string with methods or Date
Related
I logged out the value stored in one of the documents(or notes in my case) in the date key, like this
console.log(notes[0].date)
The output that I got was
and when methods like getDate() or toDateString() didn't work, what should i be doing?
You will need to convert it into a javascript date object using new Date("your mongodb date string");
See: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date for more infomation
If your dates are stored using the date data type you should manipulate them as data object in your code.
If your dates are not stored using the date data type you should migrate your data to store the date as date data type rather than string.
You then format the date, using the user’s LOCALE, only when you present it to the user.
I'm having a problem while querying for a particular date in my database
I need to get the data on a particular date
I'm using $gte and $lt mongo functions to conditionally query the date
But I'm not able to get data which corresponds to the date which i need
I'm receiving back all the data from that date till today's date
Because of the $gte(greater than or equal to) function
I'm not able to find a proper solution for this
I tried to look in stackovflw but still couldn't find it
Can someone help me!
This is the query using mongoose I'm trying to get the data only for that particular day
Attendance.find({teacherID:req.body.id,subject:req.body.subject,grade:req.body.grade , section:req.body.section,date:{"$gte": new Date(req.body.date)}}
Maybe you can read the documentation about filter by date at mongoose doc
I try to dynamically create a mongo query with dates.
This is a part of a JSON query I produced:
{"$or":[{"createdAt":{"$gte":"2017-08-31T22:00:00.000Z"}},{"modifiedAt":{"$gte":"2017-08-31T22:00:00.000Z"}}]}
but it does not work.
This is a part of code which stands behind it:
let result = {$or: [{createdAt: {$lte: new Date(date)}}, {modifiedAt: {$lte: new Date(date)}}]};
Spent 3 hours trying to find a solution. My understanding is that $lte here tries to compare mongo date object with a string describing date in ISO format. And it does not work well.
I do not know how to create a proper JSON object being a mongo query containing dates.
Please help! :-)
If you want to query Mongo with date range, you have to use a standard Date format (ISO-8601). To achieve this, try with moment:
let date ="2019-02-22"; //for example, in my apis, you can set multiple type of date (YYYY-MM-DD, YYYYMMDD, DD-MM-YYYY,..) but date format with / is forbidden.
let query = {$or:[{createdAt: {$lte: moment(date).format()}}, {modifiedAt:{$gte: moment(date).format()}}] //pass parameter to format, in this case it will use the default locale format
Like this you are setting a query. IMO querying on date range with "OR" condition is not useful: usually a date query in is "AND" condition (if you are querying on a single date, your query will be: greater than today or lower than today -> everything)
I am sending two fields from client side date and time.
date will be in the format of YYYY-MM-DD i.e 2016-11-08 and time will be in the format of 05:30 PM or 09:45 AM.
I want to combine these two fields and create new field say added_datetime , this field going to be inserted inside MongoDB, so I want it to be in the form of Mongo Date Object so that I can use it for searching with date.
Tried some random things using moments.js but unable to get what I want.
As mentioned in a similiar question you can create a date object with
var date = new Date(datestring);
The code
var startDate = new Date("1900-1-1 8:20:00 PM");
which the original questioner supplied works in chrome and should also work in node since it's the same js engine. This seems to answer your question.
You can find more on dates in the MDN documentation and MongoDB documentation.
I am querying data using OData, url looks like http://myurl.com/api/Customer?$filter=ResDate eq DateTime'2014-03-15T12:01:55.123'.
I'm getting date/time from jquery.datepicker instead of the static date and using moment.js to convert from DD-MM-YYYY to YYYY-MM-DDTHH:mm:ss in order to pass it to web service.
function convertDateToISOdate(date){
var newDate = moment(date,'DD-MM-YYYY').format('YYYY-MM-DDTHH:mm:ss');
return newDate;
}
Date returns from the function, is 2014-03-15T00:00:00.
Problem : 2014-03-15T12:01:55.123 is not equal to 2014-03-15T00:00:00, so there's no record selected.
What I want is , just to compare the date , not include time stamp.
Note : I can not change the format date/time at server side(Web service) because it's not belongs to me.
Any idea is much appreciated.
Your first call to moment(date,'DD-M-YYYY') is stripping the time information from the incoming data. try using moment(date) (no format) instead because momentjs recognizes your incoming date format intrinsically, without having to be told which format to use, and will correctly parse the H:M:S data, too.
MomentJS date parse information