Retrieving a Date object from a JSON response

Hey there, I have a situation where I'm trying to convert a JSON response into a date object in order to display it in a different format. Here's the JSON response I'm working with:

responseData: [{"id":10,"createdDate":"1 Sep, 2014 12:48:52 PM"}]

When it comes to displaying the creation date in the UI, I want it to show as 1 Sep, 2014 or in the format mm-dd-yyyy. How can I achieve this? Should I create a date object from the JSON response, or should I work on parsing the JSON response itself? Any assistance would be greatly appreciated.

Answer №1

Simply include moment.js on your website

moment(responseData[0].createdDate).format("MM-DD-YYYY");

Answer №2

Give this code a shot and see if it works

let currentDate = new Date()
let currentMonth = currentDate.getMonth() + 1
let currentDay = currentDate.getDate()
let currentYear = currentDate.getFullYear()
let formattedDate = currentDay + "/" + currentMonth + "/" + currentYear
alert(formattedDate);

Answer №3

Transform a date object after generating a JSON object in the following way -->

const parsedJson = JSON.parse('[{"id":10,"createdDate":"1 Sep, 2014 12:48:52 PM"}]'); new Date(parsedJson[0].createdDate)

Answer №4

Send the data.createdDate variable to the renderdate function for processing

function renderDate(value) {        
    var getDate;
    if (Date.parse(value)) {
        getDate = new Date(value);
    }

    //Handle JSON date format like /Date(1224043200000)/
    else {
        getDate = new Date(parseInt(value.substr(6)));
    }

    return ((getDate.getMonth() + 1) + "-" + getDate.getDate() + "-" + getDate.getFullYear());
}

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

What is the process for testing and executing the code I've written on ACE Cloud 9?

I have integrated ACE on my website to enable users to code freely. My question is, how can I execute the Python code they write? I want to add a run button in the bottom left corner (which I can style using CSS), but I'm not sure how to actually run ...

Discovering a way to extract all information from a JSON POST request using curl

Exploring the open data project on spogo.co.uk (Sport England) has been quite engaging. For a search example, check out: https://spogo.co.uk/search#all/Football Pitch/near-london/range-5. I've been experimenting with Cygwin and CURL to POST JSON dat ...

Is it advisable to segregate the Web application from the Web server for cross-platform compatibility?

One question I am contemplating is whether to have a single server serving HTML and JSON or separate them into two distinct entities. To be more specific, these are the components of my two options: 1) An API server running Python on Google App Engine ...

Experiencing challenges with showcasing numerical values in the grid cells

My latest project involves creating an interactive sudoku solver. I've successfully created a grid made up of various elements to resemble an empty sudoku grid. However, my challenge lies in getting the numbers to display within the grid cells. Click ...

Tips on invoking a Stencil component function or method from beyond the Vue instance

Is it possible to trigger a function in a Stencil component from the parent Vue instance? For example, I would like to call the callChild() method in the Vue instance, which will then trigger the doSomething() function in the Stencil component. The main ...

How can the syntax of a JSON file be verified in Unix?

How can I verify the syntax of a JSON file in Unix and then automatically move any invalid files into an error folder? Thank you, Kavin ...

Issues with the HTML required attribute not functioning properly are encountered within the form when it is

I am encountering an issue with my modal form. When I click the button that has onclick="regpatient()", the required field validation works, but in the console, it shows that the data was submitted via POST due to my onclick function. How can I resolve thi ...

Extending Two Classes in Typescript: A Complete Guide

I am looking for a way to efficiently save time by reusing common code across classes that extend PIXI classes within a 2D WebGL renderer library. Object Interfaces: module Game.Core { export interface IObject {} export interface IManagedObject e ...

Wordpress website fails to initiate Automate on Scroll (aos) functionality

I seem to be having trouble tackling any task that requires even the smallest amount of brain power. My current struggle is trying to integrate the AOS library into my Wordpress site. In an attempt to make it work, I inserted the following code into my fu ...

Encountering 'Illegal Invocation' error while running a basic script

Exploring the Speech Recognition API has been on my to-do list, so I decided to create a simple page that initiates recognition when clicking on the body element. Here is a snippet from my scripts.js file: var recognition = new window.webkitSpeechRecognit ...

Retrieves MySQL SQL findings and converts them into a JSON structure

I'm in the process of integrating Typeahead.js into my website. The Typeahead.js plugin will retrieve data from a remote page that returns JSON, similar to: http://example.org/search?q=%QUERY Here's the PHP code I've written for my site: ...

Word with the Most Points

I have created a code to determine the highest scoring word as a string, however when I calculate all words and attempt to display the results, I am encountering an issue where all results are showing as: NaN function high(x) { var words = x.split(&ap ...

next-images encountered an error during parsing: Unexpected character ''

Having trouble loading images dynamically with next-images: //Working <Image src={require(`../../images/exampleImage.jpg` )}/> However, I want to use a dynamic URL like this: //Not working <img src={require(`../../images/${image}.jpg` )}/> Th ...

Getting the value of elements with the same id in JavaScript can be achieved by utilizing the getElement

When I click on the first delete link, I want to display the value from the first input box. Similarly, when I click on the second delete link, I want to show the value from the second input box. Currently, it is always showing the value from the first del ...

Generating a JSON file by combining data from two separate lists in order to render a visually appealing stacked bar chart

Combining two lists to create a JSON for generating a stacked bar chart using JavaScript list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02 ...

Issues with my transpiled and typed TypeScript npm module: How can I effectively use it in a TypeScript project?

I'm trying to experiment with TypeScript. Recently, I created a simple "hello world" TypeScript module and shared it on npm. It's very basic, just has a default export: export default function hello(target: string = 'World'): void { ...

Adding a background image to a box in MUI is a simple task that can enhance

I've been attempting to include a background image in the Box component of mui, but I can't seem to get it to work. Here is the code I've been using: const Main = () => { return ( <Box sx={{backgroundImage:'images/cove ...

Utilizing an array as a child component in conditional rendering with vue.js

I have a Laravel API and the front end is built with Vue.js + Laravel. I am fetching data from the API and passing it to the view. Now, I want to implement conditional rendering based on certain criteria. Here's what my array looks like: "data" =&g ...

What is the best way to input information into my Google spreadsheet with React JS?

After using https://github.com/ruucm/react-google-sheets as a reference, I encountered a persistent gapi 404 error whenever I tried to run the code. It seems like the GitHub link might not exist, causing my app to fail. However, I could be mistaken. Is t ...

Unable to transmit information back to React

Recently stepping into the world of React and Node.js, I have successfully created a function in my Node.js application that executes a Python script using child process. However, I seem to be facing a challenge with my router post method named pythonExecu ...