i want to ask about the moment of a date which is recuperated from MongoDB (creation Date)
The problem is when i work with the moment to the (creation date)which is recuperated from MongoDB, i got the current date (date of today)!!
There are two screenshots that can explain more:
(https://i.stack.imgur.com/U1EPH.png)]
(https://i.stack.imgur.com/RnUod.png)
i worked with moment js and i want other propositions if found
Thank you so much
Related
I am having hard time to understand the concept of querying date in mongodb/node.js, even after going through many SO and other articles in google.
While storing the date from a react datepicker in mongodb it gets stored a day less as per my time zone but when it is fetched it is shown the same,I can understand that it is again being converted into locale
but how do query responds?
When I query mongodb from node.js/mongoose - I don't find the correct resultSo my question is while querying the mongodb - how dates are passed to mongodb ?
for example :
post.find({publish_date:{ $gte:new Date()}}
What is the new Date value - localtime zone of the server? or browser?
is the new date value converted to utc while comparing the database or compared as passed from the server ?
I have a post model where I want the post to be published on the published_date as per my timezone.
for example - if I put 2021-01-21 mongo stores 2021-01-20T18:30:00.000+00:00 - it is understood that it stored as UTC
Now when I am querying the post to be published when the server date/time is 2021-01-21 - it is not fetch any document!
When I change the publish_date as 2021-01-22 mongo stores 2021-01-21T18:30:00.000+00:00 then query produces on document.
I am not sure how to solve this problem
Any help in this regard would be helpful
Thanks.
In MongoDB all Date values are stored as UTC-Times only.
Your client application is responsible to display these UTC times as local times, if required.
MongoDB does not preserve the input time zone, if you need this for any reason then you have to store it in a separate field.
In Mongo you can display locals date/times values with $dateToString:
{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }
Whenever one has to work with date/time values then I recommend the moment.js library.
The query could be this for example:
{ $gte: moment().startOf('day').toDate() }
I am in Switzerland where we have UTC+01:00, thus moment().startOf('day').toDate() returns ISODate("2021-01-20T23:00:00Z")
I am trying to remove timestamp from a data column. I can do that in SQL Server Management Studio doing:
select CAST(GETDATE() as date)
Now, when I try to do the same thing using Nodejs-MSSQL library I get the time stamp as well:
await pool.query(` select CAST(GETDATE() as date)`)
Can someone please explain how this is even possible?
I think this question is simple really but I wasn't able to find an answer for it.
In Knex i could set a timestamp in my database with knex.fn.now()
However now I have a need to set a date 30 days after now is this as simple as knex.fn.now() + 30 work or is there another trick?
Any help is appreciated even a link to a different source.
Thank you in advance
knex.fn.now() will execute CURRENT_TIMESTAMP function on the db, which returns timestamp in ms from 1/1/1970.
You can use a db built in method for calculating future dates.
In MySQL this method calls date_add.
SELECT date_add(now(), INTERVAL 30 day);
With Knex you will need to use the raw method.
knex.select(knex.raw('date_add(?, INTERVAL ? day)', [knex.fn.now(), 30]));
Edit:
In postgress, this query will look like:
SELECT CURRENT_DATE + INTERVAL '1 day';
so, in Knex it will be:
knex.select(knex.raw(`? + INTERVAL '? day'`, [knex.fn.now(), 30]));
Thank you for the previous answer, but that Postgresql example didn't work for me.
When I tried knex.raw(`? + INTERVAL '? day'`, [knex.fn.now(), 30]), I got an error "could not determine data type of parameter"
This worked:
knex.raw(`? + ?::INTERVAL`, [knex.fn.now(), '30 day'])
I’m new to mongodb and trying to figure out sorting with MongoClient in a node.js/express application.
This works in the mongo command line client:
db.mycollection.find().sort({"date":-1}); // displays by date, newest to oldest
I’m trying to achieve the same thing in my application:
db.collection('mycollection').find().sort({"date":-1}); //order remains the same
How can I achieve the same result as the first query? Thank you.
So, first, I'd recommend using Mongoose. Failing that, however, the Node MongoClient puts things like sorting into the find() arguments, like so:
db.collection('mycollection').find({}, {sort:{date:-1}});
I have a simple XPage that is doing a partial refresh every 30 second for the demo purpose.
And randomly new Date() is returning a date that is one hour wrong.
But if I do
var d=session.createDateTime(new Date())
d.setNow()
it will return the correct datetime allways.
I've also tried printing everything on the console and the result is the same.
A database showing the problem can be found here
http://l.bitcasa.com/gco-V3cq
Anybody know what could cause this ?