What is the best way to transform a date from the dd-mm-yyyy format to the ISO date format so that it can be properly stored in

Within my mean stack application, data is organized based on dates. On the Angular side, I implemented a date picker to input/output the date for Data manipulation. The date picker generates dates in the format "dd-mm-yyyy". What is the simplest method to convert this into a format that MongoDB can comprehend, and vice versa.

Answer №1

let dateStr = "29-1-2016";
const dateArr = dateStr.split("-");    // ["29", "1", "2016"]
const newDate = new Date(parseInt(dateArr[2]), parseInt(dateArr[1]) - 1, parseInt(dateArr[0]));
                         // Date {Fri Jan 29 2016 00:00:00 GMT+0530(utopia standard time)
console.log(newDate.toISOString());
                         //2016-01-28T18:30:00.000Z

This method works fine, but is there a simpler solution available..!!

  • Can someone explain why the ISO date format shows 2016-01-28T.... instead of 2016-01-29T.....?

Answer №2

This solution worked perfectly for me:

Start by integrating Moment.js into your project code. Next, take the time string you have, like this one: var str = "29-1-2016";. Utilize moment.js along with the following code snippet:

var str = "29-1-2016";
var time = moment(str).toISOString();
\\ The variable 'time' is now an ISO string

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

VueJS - When using common functions, the reference to "this" may be undefined

I'm struggling to extract a function that can be used across various components. The issue is that when I try to use "this", it is coming up as undefined. I'm not sure of the best practice for handling this situation and how to properly assign th ...

Is it advisable to substitute setTimeout with node-schedule in a node.js environment?

How can I prevent players from entering a raffle between 11:55pm - 11:59pm every Thursday? I attempted to use node-schedule to block access during this time frame by scheduling it to run every second, but unfortunately, I was still able to access the route ...

What is the best approach for sending a single mail value to the backend when there are multiple inputs in an Axios post request?

Would like assistance with a feature where only the input fields filled by the user are sent to the backend? Here, I have 3 email input fields and want to send only the filled ones. How can this be achieved? const App =() => { const [email,setEmail] ...

Pass Form ID To Function In A Dynamic Way

I have multiple forms on my webpage and I want to use the same ajax function for all of them. It currently works well for one form by fetching the id with getElementById and then passing it to the ajax function. My goal is to dynamically pass down the form ...

Angular animations do not seem to be functioning properly with ng-show when changing the class using ng-class

I need assistance in creating a slider for a list of objects using ng-show and animations. The current setup works smoothly when the objects slide in one direction. However, I am facing issues when trying to implement the functionality for users to slide ...

Guide to managing .glb model animations in A-FRAME using Three.js

Can someone assist me with playing a glb animation in A-FRAME using Three.js? The animation works for a second and then stops. Here is my current code: <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script> <scrip ...

Display the message "Please complete all required fields" on the AngularJS href when the necessary fields have not been filled out

Is there a way to implement input validation so that when a user clicks on a href link without filling out the required input fields, they see a message prompting them to fill in the fields? I tried changing the href to a button and it worked, but that&apo ...

Maintaining checkbox state using fetch arrays

Included below is the code present on my site, pulling data for each season including numbers of home wins, win percentage, and win lsp. It functions correctly by creating a new table row for each season. Furthermore, there are two columns featuring filte ...

Enhancing Performance by Indexing Array Fields in MongoDB Documents

I'm new to this and I'm trying to index a collection of content. Here is my current query: db.contents.find({title:{$regex: /arnab/i}, tags:{$regex: /Times/i}}) Here is my index query: db.contents.createIndex({title: 1, tags:1}) Tags is an ar ...

Error in TypeScript code for combined Slider and Input onChange functionality within a Material-UI component

HandleChange function is used to update the useState for Material-UI <Slider /> and <Input />. Here is the solution: const handleChange = (event: Event, newValue: number | number[]) => { const inputValue = (event.target as HTMLInputEle ...

Mouse hovering over the JS slider activates the sliding functionality, while removing the cursor

Hi there, I'm encountering some issues with the JS clients slider on my website. I need to pause it when the mouse is over and resume when the mouse leaves. I've double-checked the code but for some reason it's still not functioning properl ...

Customize the size of data points on your Angular 2 chart with variable

In my Angular 2 application, I am utilizing ng2-charts to create a line chart. The chart functions properly, showing a change in color when hovering over a point with the mouse. However, I want to replicate this behavior manually through code. Upon clicki ...

IFrame issue: Page is constantly refreshing when on iframe source

Recently, I started working with asp.net and I'm attempting to call an Iframe src within a table row using a JavaScript click event. (i.e onclick="return loadPhaseWiseChart("DGSET00025");") Here is my code snippet: <tr height="25" id="row3" oncli ...

Restore Bootstrap Dropdown values to their initial settings when clicked

I need a button that can reset all filter dropdown values to their default values. The current code I have only changes all values to "Filter" when reset, but I specifically need it to reset to "Car brand" and "Model". Here's my code: // set.... $(" ...

Sending files via an Ajax request triggers an unexpected page refresh

I have limited experience with ajax, but I am encountering an issue when trying to send form data to an ASP controller. Scenario 1 When I set data in the ajax request to $(this).serialize() All form data is successfully posted to the controller withou ...

Tips for transferring an array between two applications using AngularJS

I have two applications, appA for front end and appB for admin end. In appA, I am building an array called queries using a service through a controller. However, when I try to retrieve this array list in a controller in appB, it appears empty. Everytime ...

Do not use npm to install underscore libraries

How can I resolve the error I encountered while attempting to install packages using npm? Here is my packages file: "dependencies": { "express": "~3.3.6", "socket.io": "0.9.16", "jade": "~0.35.0", "less-middleware": "~0.1.12", "redis ...

How can I effectively capture a change in the hour using Node.js?

Do you know of a more efficient way to monitor datetime changes instead of checking every 10 minutes, potentially firing nearly 10 minutes late? Any alternative solutions that I may have overlooked for this issue? ...

Incorrect Request Method for WCF json POST Request Leads to 405 Error (Method Not Allowed)

Hey there, I'm facing an issue with using Microsoft Visual Studio to create a WCF web service. Everything runs smoothly within Visual Studio, but when I try to connect to the service from outside, it fails to establish a connection. At first, I encoun ...

Experiencing difficulties with Magento operations

Currently facing an issue while attempting to set up Magento on my server. I have transferred all files from another server to mine, but now encountering an error. Could this be related to the symlinks I generated or is it caused by something else? Encoun ...