What is the best way to format this DateTime for a more visually appealing appearance within an Angular Material <mat-card-subtitle>?

My date looks like this:

2000-12-16T00:00:00

When displayed in Material code (as the publish_date):

<mat-card *ngFor="let book of bookItems">

  <mat-card-header >
    <mat-card-title>{{book.title | titlecase}}</mat-card-title>
    <mat-card-subtitle>{{book.description}}</mat-card-subtitle>
    <mat-card-subtitle>{{book.author}}</mat-card-subtitle>
    <mat-card-subtitle>{{book.genre}}</mat-card-subtitle>
    <mat-card-subtitle>{{book.publish_date}}</mat-card-subtitle>
    <mat-card-subtitle>{{book.price}}</mat-card-subtitle>

  </mat-card-header>

</mat-card>

How can I make the date more human-friendly?

The date is extracted from an XML file as shown below:

  <book id="B1">
    <author>Kutner, Joe</author>
    <title>Deploying with JRuby</title>
    <genre>Computer</genre>
    <price>33.00</price>
    <publish_date>2012-08-15</publish_date>
    <description>Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.</description>
  </book>

This data is fetched in the following way:

[HttpGet]
public IActionResult GetBookItems()
{
    List<BookItem> BookItems = new List<BookItem>();
    XDocument doc = _db.GetXmlDb();
    List<BookItem> bookitems = doc.Descendants("book").Select(x => new BookItem()
    {
        Id = (string)x.Attribute("id"),
        Author = (string)x.Element("author"),
        Title = (string)x.Element("title"),
        Genre = (string)x.Element("genre"),
        Price = (decimal)x.Element("price"),
        Publish_date = (DateTime)x.Element("publish_date"),
        Description = (string)x.Element("description")
    }).ToList();
    return Ok(bookitems);
}

This CRUD call is made from the Angular app to the ASP.NET Controller.

How can I enhance the display of the bookItem in the Angular app?

Here is the book interface used in the Angular app:

export interface BookItem
{
  id: string;
  author: string;
  title: string;
  genre: string;
  price: string;
  publish_date: string;
  description: string;

}

Answer №1

To transform the date value into your desired format, you can utilize Angular's DatePipes.

Before using DatePipes, make sure to convert the date value into a Date object like this:

new Date('2000-12-16T00:00:00');

For elements like <mat-card-subtitle> that require the use of DatePipe, you have the option to either apply one of the pre-defined formats

<mat-card-subtitle>{{ book.publish_date | date: long }}</mat-card-subtitle>

Or define a custom format for the pipe to follow.

<mat-card-subtitle>{{book.publish_date | date: 'dd MMMM yyyy' }}</mat-card-subtitle>

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

Angular does not completely erase everything

Having some issues with file deletion while working on angular and typescript. My setup involves three interfaces: Project, SubProject, and Position. When a subproject is added to the selected project, it gets included in the subProjectIds list of the Proj ...

"Exploring the power of D3, TypeScript, and Angular 2

I am facing challenges incorporating D3 v4 with Angular 2 (Typescript). While exploring D3 v4, I have referred to several solutions on StackOverflow without success. I have imported most of the necessary D3 libraries and typings (utilizing TS 2.0) and adde ...

Angular deep linking in an Express server is a powerful combination that allows

I'm developing a single page application using Express and Angular. One feature involves sending users an email with a link to reset their password (https://[domain].com/reset-password/[token]). However, when the user clicks on this link, it redirect ...

Changing true/false values to Yes or No in Angular array output

I am working with an array that is structured as follows: { "Tasks": [ { "TaskID": 303691, "TaskName": "Test1", "TaskType": "Internal", "Status": "Processing", "IsApproved": false, "RowNumber": 1 }, { ...

What is the best way to distinguish between administrators and regular users in an Angular project?

I am embarking on a project using Angular and I plan to incorporate both an admin and a user section within it. Is there a way to effectively separate the admin area from the user area in Angular? Additionally, how can I differentiate the style files for ...

I am sorry, but it seems like there is an issue with the definition of global in

I have a requirement to transform an XML String into JSON in order to retrieve user details. The approach I am taking involves utilizing the xml2js library. Here is my TypeScript code: typescript.ts sendXML(){ console.log("Inside sendXML method") ...

Having trouble with the "Vs Code nx console generate" command? It seems that there are no flags available to configure

My issue involves the nx console extension installed in my Visual Studio Code. Every time I attempt to use the generate command for components, services, or libraries, I receive an error message stating "ng generate @schematics/angular:component This com ...

Issues retrieving data using Ionic 4 native HTTP plugin result in returning an empty

Currently, I am working on an Ionic 4 project where I am attempting to retrieve a JSON array from a URL using the native HTTP for Ionic. However, when I attempt to fetch the data array from the URL in JSON format, I am met with an empty page. The JSON dat ...

The URL "http://packagist.org/p/provider-3.json" was unable to be retrieved due to a bad request error (HTTP/1.1 400 Bad Request)

Encountering a 400 bad request issue when trying to connect over HTTP, despite the package only being installable via HTTP. Attempting to override in composer.json to force HTTPS as suggested by others for a workaround, but that solution doesn't seem ...

Exploring the Concept of Dependency Injection in Angular 2

Below is a code snippet showcasing Angular 2/Typescript integration: @Component({ ... : ... providers: [MyService] }) export class MyComponent{ constructor(private _myService : MyService){ } someFunction(){ this._mySer ...

Javascript Library Issue: "Implicitly Declared Type 'Any' Error"

I am currently in the process of developing a JavaScript library that will interact with an API. My goal is to create a module that can be easily published on npm and utilized across various frameworks such as Angular or React. Below is the code snippet fo ...

"RxJS in Angular 2: The elusive map function seems to be missing

Issue: Received an error stating, "Property 'map' does not exist on type 'Observable'." import { Component } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; decl ...

What is the best way to keep track of a checkbox's value after unchecking it and then returning to the same slide?

Issue: By default, the checkbox is always set to true in the backend code. Even if I uncheck it using JavaScript, the value remains as true when switching between slides. Desired Outcome: If I uncheck the checkbox, the updated value should be saved so tha ...

Utilizing dependency injection for nested services in Angular 2

I am designing a custom Broadcast system to create and utilize EventEmitters dynamically within the user context by injecting an EmitterService. @Injectable() export class BroadcastService implements OnInit { private _emitters: { [channel: string]: Ev ...

Deploying an Angular application on AWS EC2 without using nginx as a reverse proxy

Our team has been tackling the challenge of hosting an Angular application on AWS. A question has emerged: can we deploy the Angular application without relying on nginx? This inquiry arose when we successfully deployed a node.js application without any ...

A single click is required for Observables to load on an HTML page

While working on my Angular web application, I encountered an issue with displaying data when using Observables and Subjects. Typically, when searching the Firebase DB, I use *ngFor="let myvar of _myvar | async" in my HTML to display the retrieve ...

Mastering the art of bi-directional data binding with nested arrays in Angular

Imagine you have a to-do list with various tasks, each containing multiple subtasks. You want the ability to change the subtask data, but why is Angular not properly two-way binding the data for the subtasks? HTML <div *ngFor="let task of tasks"> ...

Difficulty with Angular update: TS2339 error stating that a property is not recognized on a imported object within a JSON

Recently, I encountered an issue while upgrading Angular from version 7 to 16. The import of objects from a JSON file was functioning properly in Angular 7 but is now causing errors. Specifically, it throws the error message "TS2339: property does not ex ...

Reusing observables after encountering errors

Is there a way to handle an Observable that errors out and stops emitting values in Angular templates? For instance, if I have a Subject that switches to an HTTP call using switchMap and the call fails due to incorrect user input. How do I ensure that the ...

Creating an Angular component to display a dynamic table using ngFor directive for a nested JSON data structure

Currently diving into Angular (version 8) and grappling with the following JSON structure {layer1 : [ { id: 'lay1', name: 'first layer', results: [ { rows: ...