Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set input field with current date in angular

Tags:

html

angular

I want my form date input field to display today's date e.g (2019-11-28) when I open the form. I saw many examples on how it works in AngularJs but how can I get the date with reactive form.

form

//.....
<div class="form-group">
  <label>Date:</label>
      <div class="input-group">
        <input type="date" class="form-control col-md-8" formControlName="date"/>
      </div>
</div>
.
//...

With formControlName.

.ts

//...
currentD = new Date();

..
..
//....
like image 589
Pleasure Avatar asked Oct 20 '25 03:10

Pleasure


2 Answers

Set formControlName default value as (new Date()).toISOString().substring(0,10)

Working Demo

Try like this:

.ts

myForm:FormGroup;

this.myForm = new FormGroup({    
 'presentDate': new FormControl((new Date()).toISOString().substring(0,10))
});
like image 150
Adrita Sharma Avatar answered Oct 21 '25 17:10

Adrita Sharma


use data bindind you can use one way binding

<input type="date" [value]="currentD" class="form-control col-md-8" formControlName="date"/>

or tuo way binding

<input type="date" [(ngModel)]="currentD" class="form-control col-md-8" formControlName="date"/>
like image 41
Hamza Meghlaoui Avatar answered Oct 21 '25 16:10

Hamza Meghlaoui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!