What is the best way to convert a date to ISO format for MongoDB with the help of moment.js?

Would someone be able to assist me with converting the date mentioned below into ISO format for MongoDB?

  var d = { storedDate: '26/06/2020 05:55:29 PM' };

I have been struggling to find the correct parameter needed to achieve the desired format. I attempted the following code snippet.

moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");

Is there a way to obtain it in the form of

ISODate("2020-06-26T17:55:29.274Z")

Your guidance on this matter would be much appreciated.

Answer №1

By using the moment function with the appropriate date format, you can convert a stored date value to an ISO date in UTC for use with MongoDb.
Remember to specify the input date format when calling the function.

Answer №2

For those looking to accurately store a Date object, consider this approach:

moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()

Avoid storing date and time values as strings; always opt for the appropriate data type.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to effectively manage Mongoose Query Exceptions in Express.js

Let's imagine a scenario where I need to execute a Mongoose query in an Express post route: app.post("/login",(req,res)=>{ const username = req.body.username const password = req.body.password User.find({username:username},(er ...

Displaying empty values as zeros

Currently, I am creating a chart using Morris js. The data for the chart comes from a server and consists of dates and values. However, there are cases where certain dates do not exist in the data set. In such situations, I want to display these non-existe ...

JQuery animations not functioning as expected

I've been attempting to create a functionality where list items can scroll up and down upon clicking a link. Unfortunately, I haven't been able to achieve the desired outcome. UPDATE: Included a JSFiddle jQuery Code: $(document).ready(function ...

Flash notifications are failing to display in the express/nodejs/ejs setup

I've been troubleshooting my flash messages for an hour now, but they just won't seem to work. I'm sure there's something obvious that I'm missing, but no matter what I try, nothing seems to fix it. Here is my Middleware setup: / ...

The issue of unreadable URLs on the server side caused by a Javascript Ajax problem

I have successfully implemented code to send information from a website to a server. The code I used is as follows: <!DOCTYPE html> <html> <script> var Network = {"name":"", "password":""} var json ...

Navigating to dashboard upon successful validation in VueJS is a crucial step that ensures user

How can user input validation be implemented before redirecting to the dashboard? The code provided below: The code below is currently loading the dashboard first, but it should display the login page first. After validation, it should redirect to the das ...

No Results Returned by Sails Query Following count() Query

Upon execution, the following code returns empty results. Although the correct values are retrieved without the Count query, the final response remains empty. Could this issue be related to a race condition? module.exports = { getSites: function (req, res ...

Ways to effectively go through local storage using a loop

I'm currently working on enhancing my navbar by adding links based on searches made by users and their favorite selections. My goal is to avoid duplicate entries in the "past searched" section if the current search already exists in the list. I'm ...

Utilize Express for Redirecting and Rendering

Upon hitting my API, I need to change the URL from https://myapp.herokuapp.com/token/aaa.bbb.ccc to https://myapp.herokuapp.com/messages/:id. Additionally, I want to display the message view. Code: app.get('/token/:id' , (req, res) => { va ...

What is the best way to incorporate caching for AJAX responses, specifically in Internet Explorer?

While most people focus on preventing AJAX caching in IE, I am interested in implementing this technique for other browsers. I have attempted to use HTTP headers without success and am now feeling confused. Any assistance would be greatly appreciated. ...

The JSON response did not contain the expected property in Javascript

Currently, I am developing a weather widget using React that displays temperature and rain forecast. The data is fetched from OpenWeather API and the JSON response structure is as follows: //rainy day 0:{ main: { temp:10} rain: { 3h: 1000} } / ...

How to implement variables within the .map() function in JavaScript?

I am working on a map function where I need to pass in a variable as a Key to change the object item key to be based on that variable instead. For example, I want the obj.Salary below to actually represent the salary value when day equals "Day" instead o ...

Only make an AJAX request for suggestions with jQuery Autocomplete if the item does not match any data from the previous request

I am currently working with jQuery Autocomplete functionality. The issue I am facing is that it triggers an AJAX request every time a key is pressed, which is not desirable. Ideally, if the data from a previous AJAX request already matches the search query ...

How can you extract a string from the xmlhttp.send(str) method using POST in PHP?

Here is the code snippet I am currently working with in client-side JavaScript: xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST",url + page,true); xmlhttp.send(str); However, I am now faced with the challenge of extracting this string on the PHP side. I ...

Incorporating a JavaScript script into my Angular 7 project

My project requires me to incorporate the "Loadingbar.js" library into one of my pages. You can find this library here: . Initially, I inserted the CSS code into my global "style.css" file. I started by placing the script in my index.html file: <script ...

Using client-side javascript to retrieve the NPM version tag

Is there a way to retrieve the most recent version tag of an NPM package using client-side JavaScript? I have searched for APIs and other resources, but everything I found seems to be outdated. Nevertheless, it appears possible because shields.io can do t ...

Using express alone with mongoose will only provide the id field in the

The User Model: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const UserSchema = new Schema({ email: { type: String, unique: true, required: true, trim: true }, username: { type: String, uni ...

Discovering and revising an item, delivering the complete object, in a recursive manner

After delving into recursion, I find myself at a crossroads where I struggle to return the entire object after making an update. Imagine you have an object with nested arrays containing keys specifying where you want to perform a value update... const tes ...

Variable scope not properly maintained when there is a change in the Firebase promise

I am currently working on developing a controller function to handle signup submissions using Firebase. However, I've encountered an issue where the variables within the scope (controllerAs: $reg) do not seem to update correctly when modified inside a ...

Necessary tooltip shown when closing modal on FireFox

I'm facing a unique challenge while building an AngularJS app that is specifically designed for Firefox. The problem arises with the tooltip on a modal when the user clicks the Close button. Upon opening the modal, there is a dropdown list that is in ...