What is the best way to change the date format in the GET API response for all objects

Recently started working with Angular, I'm receiving object data from an API call.

    data= [
  {
    "Id": 4,    
    "IssueDate": "2021-01-25T15:17:00.85",
    "ExpiryDate": "2021-01-25T15:25:40.263",
  },
  {
    "Id": 5,
    "IssueDate": "2021-01-25T15:25:40.263",
    "ExpiryDate": "2021-01-25T15:25:40.263"
  }
]

I've noticed that the IssueDate and ExpiryDate are in different formats. Is there a way to change both dates to DD-MM-YYYY format?

Answer №1

If you wish to display dates on the user interface, utilize the date pipe. If not, implement the following code snippet:

this.data = this.data.map(d => ({ ...d, IssueDate: new Date(d.IssueDate), ExpiryDate: new Date(d.ExpiryDate) }))

Answer №2

Make sure to include the code snippet below in your TypeScript file along with your response.

const information = [
  {
    "Id": 4,    
    "IssueDate": "2021-01-25T15:17:00.85",
    "ExpiryDate": "2021-01-25T15:25:40.263",
  },
  {
    "Id": 5,
    "IssueDate": "2021-01-25T15:25:40.263",
    "ExpiryDate": "2021-01-25T15:25:40.263"
  }
];

information.map((data) => {
   const issued_date = new Date(data.IssueDate);
   const expiration_date = new Date(data.ExpiryDate);
   data.IssueDate = `${issued_date.getDate()}-${issued_date.getMonth()+1}-${issued_date.getFullYear()}`
   data.ExpiryDate = `${expiration_date.getDate()}-${expiration_date.getMonth()+1}-${expiration_date.getFullYear()}`
  
});

console.log(information);

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

(React) Error: Unable to access property 'this' as it is set to undefined while trying to utilize refs

When using a ref in React to refer to an input field, I encountered an error: "Uncaught TypeError: Cannot read property 'this' of undefined". Despite having defined the ref, React seems unable to locate it in my code. What could be causing this i ...

The animation speed of the jQuery hashchange event is set to zero, causing the animation

I'm facing an issue with jQuery where my animation inside a hashchange event is not smooth; it happens instantly when triggered. I'm looking for a way to make the animation smoother. jQuery( document ).ready(function() { jQuery(window).on(&a ...

Angular Material: creating a custom sidenav transition from detailed layouts to icon-based designs seamlessly without the need for predefined

I'm currently working on a <sidenav> that has the ability to toggle between displaying text along with icons or just icons using a menu button. The <sidenav-content> section is set to automatically resize with a smooth transition effect. ...

What is the best way to create a scrollable tbody in an HTML table using React?

In my current project, I am using React, Node, Express, and Postgres to fetch data from a database. The issue I'm facing involves creating a scrollable table within a div that spans the entire screen width. Initially, I had to apply display: block to ...

Utilize the click functionality in Angular along with Openlayers to identify the specific layer that has been clicked

Currently, I am working on an openlayers map project and need a way to identify the layer I am clicking on. This information is crucial as I plan to retrieve specific data from geoserver regarding the clicked layer. In the past, I managed to achieve this ...

Can the line "as is" be included in HTML code?

Can I incorporate the following JavaScript and JSON expressions directly into HTML code? CONTRATE > 50 && SALINC <= 50 || RETAGE >= 50 { "CONTRATE": 0, "SALINC": 0, "MARSTATUS": "single", "SPOUSEDOB": "1970-01-01" } I want to be able to ...

What is the best way to transform an Observable array containing objects into an Observable that emits the data contained within those objects?

Encountering an error: Error: Type 'Observable<Country[]>' is not assignable to type 'Observable'. Type 'Country[]' is missing properties like name, tld, alpha2Code, alpha3Code and more.ts(2322 The issue might be due ...

Adding query parameters dynamically in Vue without refreshing the component

I'm attempting to update the Query Parameters in Vue without refreshing the component using Vue-Router. However, when I use the code below, it forces a component reload: this.$router.replace({query: this.filters}); Is there a way to prevent the comp ...

What is the best way to duplicate/rename/replace an image in PHP while utilizing form submission and radio buttons?

I am new to PHP and I am working on a feature that allows a user to change an image using radio buttons and form submission. The goal is to copy and rename one of two image files (Open/Closed.jpg) to another location (images/status/Status.jpg) without requ ...

The checkbox click function is not functioning properly when placed within a clickable column

In my coding project, I decided to create a table with checkboxes in each column. <table class="bordered"> <thead> <tr style="cursor:pointer" id="tableheading" > <th>Name ...

What causes the "500: Unknown web method" error when JavaScript tries to call a Page WebMethod?

I am encountering an issue with a method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories() { var business = new CategoryBusiness(); var categories = business.ListRootCategories(); return categories.Json(); } Additiona ...

FlexiGrid issue: WebMethod failing to execute

I'm encountering an issue with FlexiGrid in my project. The problem lies in the WebMethod not firing properly, specifically during a Json/Ajax call. Despite setting a debug point at the WebMethod and confirming the correct URL through Firebug, it stil ...

What is the best way to implement a promise function in this scenario using JavaScript?

I am currently working on an app using AngularJS and the MediaFire JavaScript SDK to perform various tasks. One specific task I am struggling with is creating a folder during an upload process, which returns a 'folderkey'. I would like to use .t ...

Steps for setting up Angular 5 on your system

My current project requires Angular 5, but with the recent release of Angular 7, I'm unsure how to set up Angular 5 on my computer. I had transitioned from Angular 6 a few months ago. Do I need to uninstall the latest version to work with Angular 5? ...

An issue occurred while attempting to retrieve information from the database table

'// Encounter: Unable to retrieve data from the table. // My Code const sql = require('mssql/msnodesqlv8'); const poolPromise = new sql.ConnectionPool({ driver: 'msnodesqlv8', server: "test.database.windows.net", ...

Utilizing numerous instances of setInterval

I've created a jsFiddle which can be found here: http://jsfiddle.net/dztGA/22/ Main Objective: My aim is to have two independent timers on the same page that can be stopped and restarted either by hovering or manually. The Issue: The problem illustr ...

OpenLayers: real-time data display of objects from a list

When working with OpenLayers, I encountered an issue where my object was undefined and I couldn't retrieve the information I needed to display feature data on the map. Initially, I passed a list from the controller to my JSP file and attempted to use ...

"Encountering a 404 Not Found error while attempting to access Angular

Welcome to my index.html file! <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Discover AngularJS2</title> <!-- bootstrap --> ...

Loading the children of a specified div ID using AJAX

Imagine having the following code: $('a').each(function() { $(this).click(function(e) { e.preventDefault(); var href = $(this).attr('href'); $('#somediv').load(href + ' #foo'); }); }) ...

Tips for accessing the data type of a nested property in TypeScript

If I want to keep the following declarations as they are, how can I specifically retrieve the type of the members property? export type Members = { __typename?: 'Members'; id?: Maybe<Scalars['String']>; ... }; export type P ...