What is the best way to show the stored date in the input field using the GET method in mat-datepicker?

After creating a mat-datepicker and saving the selected date in the back-end, I am now wondering how to display the saved date in the input field.

Below is my code snippet:

// HTML
 <mat-form-field appearance="fill">
   <mat-label>Datum</mat-label>
      <label>
        <input matInput [matDatepicker]="picker" formControlName="financial_year_start" required>
       </label>
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker></mat-datepicker>
          <mat-error *ngIf="settingsContentForm.get('financial_year_start').hasError('required')">Input is required!</mat-error>
 </mat-form-field>
// TS
dateForm: FormGroup;
 ngOnInit() {
 this.dateForm = this.formBuilder.group({
      financial_year_start: [null, Validators.required],
    });
}

// My JSON Data
{
    "success": true,
    "mandant": {
        "mandantId": 1,
        "firm": "Test Ltd.",
        "financial_year_start": "July 1, 2018" // Display this value in the input field as DD-MM-YYYY format 
    }
}

// Service for GET Date
public getCurrentMandantData(): Observable<any> {
    const url = `/current-mandant`;
    return this.http.get<any>(`${environment.baseUrl}` + url);
  }

Answer №1

To accomplish this task, follow these steps:

  1. Inside the component.ts file, declare a variable to hold the date value:

    myDate = new Date();

This will initialize the date with the current date. The value can be changed later on. 2. If you are working with observables and need to assign the value to the date, use the toDate() method:

this.myDate = this.dateFromObservable.toDate();
  1. Connect it to the template file (html) using the [startAt] binding:

    <mat-datepicker [startAt]="myDate">

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

Don't continue to expect other promises if one of them comes back with a special

I am dealing with a situation where I need to validate data in multiple tables by utilizing a function called validateTables(). This function relies on an asynchronous helper function queryTable() to check each table through API queries. The requirement fo ...

How to handle type errors when using properties in Vue3 Single File Components with TypeScript

I've hit a roadblock while attempting to utilize properties in Vue3. Despite trying various methods, I keep facing issues during the type-check phase (e.g.: yarn build). The project I'm working on is a fresh Vue3-ts project created using Vite. B ...

Transferring image data from Angular to ExpressJS using blobs

While attempting to send a blob image, I encountered an Error: Unexpected end of form when using multer with Serverless Framework. Upon checking console.log https://i.sstatic.net/s8VSs.png My understanding is that I need to append it to FormData before s ...

What is the best way to customize the styles of Material UI V5 Date Pickers?

Attempting to customize Mui X-Date-Pickers V5 through theme creation. This particular component is based on multiple layers. Interested in modifying the borderColor property, but it's currently set on the fieldset element, so need to navigate from Mu ...

How to Efficiently Remove Array Elements by Index in Typescript

What is the best way to remove an item by its index using Typescript? For example: let myArray = ['apple', 'banana', 'cherry', 'date']; // How can I delete the item at index 2? ...

What could be causing the issue: Unable to locate or read the file: ./styles-variables?

I'm currently following a tutorial on how to create responsive layouts with Bootstrap 4 and Angular 6. You can find the tutorial here. I've reached a point where I need to import styles-variables.scss in my styles file, but I keep encountering t ...

How to implement Dragula and Angular to enable draggable elements to be dropped in any position within their container?

I am in the process of developing a web application using Angular, which incorporates dragula. While the current drag and drop functionality works well for moving items within a designated "bag", I am seeking to enhance it with more advanced features. Is ...

What steps are involved in creating a local unleash client for feature flagging?

Currently attempting to implement a feature flag for a Typescript project using code from the Unleash Client. Here is where I am creating and connecting to an instance of unleash with a local unleash setup as outlined in this documentation: Unleash GitHub ...

Angular 8 Observable: Managing User Objects

I recently developed a new service and attempted to call an API method (HTTP GET) to retrieve data, but I'm encountering an issue where not all the data objects from the API GET request are being displayed. angular-component.ts public allUsers$: Obs ...

Angular button debounce time upon click

I'm just getting started with RXJS and I'd like to add debounce time when clicking on a button. Here's the code for my button: <button mat-mini-fab (click)="send()" [disabled]="!message.text || message.text.length === 0 || ...

Retrieving input values using alert-controller in Typescript for Ionic framework and Angular

I am working with the Ionic (angular) framework and I need to extract information from the alert-controller inputs in order to utilize them within a function. Is there a method for accomplishing this? async presentAlertPrompt(resp) { const alert = await ...

Exclude file from the source directory in Webpack configuration

My Angular project is set up with webpack for compilation, but I am facing an issue with separate main files for different environments (main.dev-only.ts, ...). Whenever I try to compile, I encounter the following error: ERROR in : Type in is part of the ...

I am facing difficulty in deploying my Next.js app with Firestore

Having trouble deploying my application using npm run build, Vercel, or another service. It works fine locally without showing any errors. I am using Firebase, Next.js, and TypeScript. The issue seems to be related to getStaticProps. When requesting data, ...

What is the best way to bring a local package into another npm package and verify its functionality using Typescript?

In a scenario where there are 3 npm projects utilizing Webpack and Typescript, the folder structure looks like this: ├── project1/ │ ├── tsconfig.json │ ├── package.json │ ├── src/ │ │ └── index.ts │ ...

Angst with the Angular Command Line Interface

After installing the Angular CLI via the command prompt, I encountered an error related to @angular-devkit/build-angular. However, everything else seems to be working fine. Does anyone have any ideas as to why this might be happening? https://i.stack.im ...

What is the best way to convert one array of types to another array of types in Typescript?

Imagine you have the following: type AwesomeTuple = [string, number, boolean] Now, you're looking to transform that type using a generic approach: type AmazingGeneric<T extends any[]> = ... In this scenario: AmazingGeneric<AwesomeType> w ...

How can I generate an element with a dynamic name in ngModel?

Is there a way to dynamically assign a name to an element in ngModel? <div class="form-group" *ngFor="let setting of settings"> <label [for]="setting.name + '-' + setting.type">Setting</label> <input type="number" c ...

Invalid data in JSON file causing issues in Vue.js

In my vuex store, I store all the translations for my application. These translations are imported from a JSON file in the following manner: import en from '@/locales/en-US.json'; const question: Module<State, any> = { namespaced: true, ...

Angular 4: Transform a string into an array containing multiple objects

Recently, I received an API response that looks like this: { "status": "success", "code": 0, "message": "version list", "payload" : "[{\"code\":\"AB\",\"short\":\"AB\",\"name\":\"Alberta&b ...

Encountering a snag when trying to load JavaScript within an HTML document

I encountered an error while trying to load an HTML file in the JavaScript console of the Brave browser. The error message reads: require.js:5 Uncaught Error: Module name "constants.js" has not been loaded yet for context: _. Use require([]) https://requir ...