Is there a way to transform a date format like "22-07-2020 12:00" into ISO date format?

Can anyone help me convert a date from this format:

22-07-2020 12:00

to the following format:

2020-07-07T11:39:02.287Z

I'm unsure how to achieve this, any advice would be appreciated. Thanks!

Answer №1

Below is a handy function that converts a string containing date and time in the format YYYY-MM-DD hh:mm:ss to an ISO date:

function toIsoDate(dateTime){
    const date = dateTime.split(" ")[0].split("-");
    const time = dateTime.split(" ")[1].split(":");
    return new Date(date[2], date[1]-1, date[0], time[0], time[1]);
    // or if you want to return ISO format as a string
    return new Date(date[2], date[1]-1, date[0], time[0], time[1]).toISOString();
}

The Date object accepts parameters in this order:

(year, month, day, hours, minutes, seconds, milliseconds)
. It's important to note that the value for the month parameter ranges from 0 to 11, not 1 to 12. That's why we subtract 1 from the month (date[1]) within the function.

const dateTime = "22-07-2020 12:00";
console.log(toIsoDate(dateTime));

2020-07-22T12:00:00.000Z

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

Using JavaScript to extract variables from parsed JSON data

Could someone please help me understand how to run this code smoothly without encountering any errors? var test = 'Something'; JSON.parse('{"xxx": test}'); I am inquiring about this because I have a JSON object containing variables th ...

execute ajax within a standalone javascript function

I am just starting to learn about jquery and ajax, and I have a specific requirement to call ajax from a separate javascript function. The issue is that the JSP file is dynamically generated and the button IDs in the JSP file are also created using a for l ...

What could be causing my ASP.Net MVC script bundles to load on every page view?

I'm a bit puzzled. The _layout.cshtml page I have below contains several bundles of .css and .js files. Upon the initial site load, each file in the bundles is processed, which makes sense. However, every time a new view is loaded, each line of code f ...

Pair of Javascript Functions Using Async with Parameters

This question builds upon a previous inquiry raised on Stack Overflow: When considering approach number 3 (referred to as the "counter" method), how can we ensure that the function handleCompletion can access necessary data from startOtherAsync to perform ...

Comprehending the concept of error and data callbacks in functions

I'm feeling a bit puzzled about how the function(err, data) callback operates. Is the first argument always designated to handle errors? And what happens with the rest of the arguments in a scenario like function(x, y, z, a, b, c)? How exactly does ...

Error in AngularJS: Unable to access property 'get' as it is undefined

Upon examining my App, I found that it is structured as follows: var app = angular.module('cockpit', ['tenantService', 'ngMaterial', 'ngMdIcons']); The controller associated with my App appears like this: angula ...

Successfully Passing GET Parameters to ngResource in AngularJS

I'm struggling to figure out how to pass a simple id parameter to my created resource. The service in question is: angular. module('shared.testUser'). factory('TestUser', ['$resource', function($resource) { ...

Having Trouble Rendering EJS Files in HTML: What Am I Doing Wrong?

I'm having trouble displaying my EJS files as HTML. Whenever I try to access my EJS file, I receive a "Cannot GET /store.html" error message. if (process.env.NODE_ENV !== 'production') { require('dotenv').config() } const stri ...

Having trouble modifying the Input with split() in angularJS

I am faced with a nested JSON object that contains an array as one of its properties. Each item in the array is separated by a ';'. My goal is to use ';' as a delimiter to split each array item and make necessary changes. However, I am ...

Creating a custom video to use as the favicon for my website

Imagine this: With the help of this plugin, you can have a video playing as your site's favicon using the following code snippet: var favicon=new Favico(); var video=document.getElementById('videoId'); favicon.video(video); //stop favicon.v ...

Experiencing Issues with File Downloading on Express Server with Axios and Js-File-Download Library

I developed a feature on my express server that allows users to download a file easily. app.post("/download", (req, res) => { let file_name = req.body.name; res.download(path.join(__dirname, `files/${file_name}.mp3`), (err) => { ...

Problems arise when using $(window).width() in conjunction with scrolling functionality

I am attempting to ensure this code only activates when the device window exceeds 960px and triggers when the window scrolls down 700px. The second condition is functioning as intended, but the first condition is not working properly. The code functions f ...

How do three buttons display identical content?

I have three buttons on my website, each with its own unique content that should display in a modal when clicked. However, I am experiencing an issue where regardless of which button I click, the same content from the last button added is displayed in the ...

When hosted, OpenCart encounters a JavaScript error stating that the property "document" cannot be read because it is null

After successfully running opencart on my local machine, I encountered some errors upon uploading it to the hosting/server. The specific error message is as follows: Uncaught TypeError: Cannot read property 'document' of null f.each.contents @ j ...

What are the steps to build a dynamic webpage in a Django project?

For my Django app, I have successfully created static pages without any ajax requests. However, I now want to add a dynamic subpage that can change its content without refreshing or reloading the entire page. Let me explain what I'm trying to achieve ...

Implementing personalized validation post-control initialization in an Angular 4 reactive form

I have been working on an Angular 4 application and using reactive forms. For one of my forms, I am trying to implement custom validation on a control after it has been initialized, based on whether a checkbox is checked or unchecked. CODE form.componen ...

What could be causing my jQuery to not retrieve the value of the <select> element?

Here's a snippet from my index.html file: <body> <select id="car"> <option value="TOYOTA">TOYOTA</option> <option value="BMW">BMW</option> </select> <input type=button id="get_btn" valu ...

Issue loading a 300 MB file into BigQuery results in a timeout error

Trying to implement the Node.js example shown in the data post request section (located towards the end here: https://cloud.google.com/bigquery/loading-data-post-request) has hit a snag when dealing with larger files. While the sample code functions proper ...

Template not rendering array data correctly in Meteor

Here is an example of the array structure: var myarray = [ device1: [ name:device1 , variables: [ variable1: [ name: variable1, unit: "a unit", ...

Oops! Encounter an issue while trying to deploy VueJS App on Firebase Hosting

I recently developed a Vue application and integrated firebase tools to deploy it on Firebase hosting. Initially, everything worked fine when I ran npm run build and firebase deploy. However, upon making changes and attempting to use commands like npm run ...