Michael
Michael

Reputation: 13636

Show current date in html in angular component

I use angular 8. In my html I need to display current date.

Here is html code:

   <label class="col-md-4">Creation date </label>
   <input type="date" class="form-control" formControlName="date"/>

How can I display in html above current date?

Upvotes: 1

Views: 2663

Answers (2)

Adrita Sharma
Adrita Sharma

Reputation: 22203

For reactive form, you can try like this:

myForm:FormGroup;

this.myForm = new FormGroup({    
 'date': new FormControl((new Date()).toISOString().substring(0,10))
});

Working Demo

Upvotes: 3

Sajeetharan
Sajeetharan

Reputation: 222702

In your component.ts file, you can get the date as,

export class AppComponent implements OnInit {
   dateVal = new Date();

and display in the component.html, if you want to format you can use DatePipe as you need

{{dateVal | date: 'M/dd/yyyy'}}

If you need to display in input, you can do

<input type="date" [ngModel] ="dt | date:'yyyy-MM-dd'" (ngModelChange)="dt = $event">

DEMO

Upvotes: 3

Related Questions