Hi Aman,
But whenever the date/time is inserted its off.
Could you specify what you mean by “off” in this context?
If I understand correctly, you’re trying to insert the current datetime into a document. However, Date.now() will give you the client’s datetime instead of the servers. Also, moment.utc(Date.now()) does not return a date object as understood by MongoDB. Rather, it will return a moment object. I believe what you want is similar to:
timestamp: { type: Date, default: new Date(moment.utc(Date.now())) }
The new Date(...) will convert the moment object into a MongoDB Date object which can then be recognized by MongoDB, e.g. for use in TTL index.
MongoDB store dates in UTC by default.
If you want you can use new Date() function to insert the current datetime of mongo server’s time zone.
Actually it will insert the client’s time instead of the server’s time. This is because the expression new Date() was evaluated client-side (since it’s a Javascript expression) before it was sent to the server.
Best regards,
Kevin