How to set input type date to the today's date in angula js

  • This is my date field

                        <input type="date" class="form-control"  [(ngModel)]="form1.date" name="date">  
    
  • I want to show the today’s date by default I have tried using this but its didn’t work for me

                        {{ today | date }}  
    
  • where I went wrong ?

  • is there any other way to do this , please tell me ?

Hi @xcfx2888, for a default value with ngModel you have to give the referenced property a default in the component. I don’t know where exactly form1 and today are coming from, basically something like this:

@Component({
  // ...
})
class MyComponent {
  today = '2019-01-05'

  form1 = {
    date: this.today
  }
}

If you want to use the date pipe, you can inject it in your component like so:

import { DatePipe } from '@angular/common'

@Component({
  // ...
})
class MyComponent {
  get today () {
    return this.datePipe.transform(new Date(), 'yyyy-MM-dd')
  }

  form1 = {
    date: this.today
  }

  constructor (private datePipe: DatePipe) { }
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.