[Fixed] How can I get selected date in Angular?

Issue

I want to get selected date in input field which is type date.
Here is my html code

 <div class="col-md-5">
        <label>Rent Start Date</label>
        <input type="date" style="width: 100%;" class="mb-3" [ngModel]="startDate | date:'yyyy-MM-dd'"  (ngModelChange)="startDate = $event"  name="startDate"><br>
        <label>Rent End Date</label>
        <input type="date" style="width: 100%;" class="mb-3" [ngModel]="endDate"   name="endDate" ><br>
        <button class="btn btn-success">Rent This Car</button>
    </div>

I used ngModel but it does not work. When I print the selected date in my component result is undefined.
component code.

 startDate:Date;

   endDate:Date;
printDate(){
      console.log(this.startDate);
      console.log(this.endDate);
    }

Solution

Your code should work fine you are just missing the ngModelChange binding on the end date.
Here is a stackblitz where everything works as you want.

Leave a Reply

(*) Required, Your email will not be published