26

I'm using a date picker from angular material. I want to set a default value but it is not showing the value.

<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
   <input matInput [picker]="picker" placeholder="Date"
                  autocomplete="off"
                  name="date" 
                  formControlName="date">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker  [startAt]="startDatePicker" #picker></mat-datepicker>
</mat-form-field>

this is my .js code with the value that i want to set as default

var date = this.datepipe.transform((new Date().getTime()) - 3888000000, 'dd/MM/yyyy'); 

this.form = this.formBuilder.group({
        dataInicial: [data_inicial],
                   ...
User3250
  • 2,961
  • 5
  • 29
  • 61
ssct79
  • 373
  • 2
  • 9
  • 17

8 Answers8

27

This works for me!

HTML-

<mat-form-field>
    <input matInput [matDatepicker]="picker1" placeholder="From Date" [formControl]="date1">
    <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
    <mat-datepicker  #picker1></mat-datepicker>
</mat-form-field>

TS-

date1 = new FormControl(new Date())
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Rohit Parte
  • 3,365
  • 26
  • 26
18

You need to provide a Date object to the startAt change as below:

In .ts:

date = new Date((new Date().getTime() - 3888000000));

In html:

<mat-datepicker  [startAt]="date" #picker></mat-datepicker>

A working demo here: https://stackblitz.com/edit/angular-n9yojx

User3250
  • 2,961
  • 5
  • 29
  • 61
  • hi! thanks for the answer but it still does not showing :/ – ssct79 Oct 15 '18 at 02:52
  • 1
    @AlexLungu Not sure about ur case. Did you check the demo stackblitz? It works! – User3250 Dec 04 '18 at 15:58
  • You are right. I missunderstood the question. I was looking for something more like [min]. As in the calendar "startsAt" a specific date. That's not how it works though. Sorry about the misunderstanding – Alex Lungu Dec 06 '18 at 12:43
  • @AlexLungu That's fine. Post a new question if you still facing issue. – User3250 Dec 06 '18 at 14:04
8

You can use the formControl defined and in your input.

here is html

<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
    <input [matDatepicker]="picker" matInput placeholder="Date" autocomplete="off" name="date" formControlName="date" [(ngModel)]="date.value">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
          <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

here is TS declare you formControl

date: FormControl;
this.date = new FormControl(new Date(<you can provide you date input field if you getting date from other sources>))
Irrfan23
  • 362
  • 5
  • 17
3

In a group of form controllers, you can use the default date as seen in follows.

 birthdayCtrl: ['1999-01-31']
Sandun Susantha
  • 1,010
  • 1
  • 10
  • 11
2

Below is working code with Angular 10, Angular Material 10 and Reactive forms

component.ts

      this.youForm= this._formBuilder.group({


        StartDate: [new Date(), Validators.required],

      });

No change on component.html

  <mat-form-field appearance="outline">
            <input matInput [matDatepicker]="StartDate" placeholder="Start date *" 
              formControlName="StartDate">
            <mat-label>Start Date *</mat-label>
            <mat-datepicker-toggle matSuffix [for]="StartDate"></mat-datepicker-toggle>
            <mat-datepicker #StartDate></mat-datepicker>
          </mat-form-field>
MJ X
  • 8,506
  • 12
  • 74
  • 99
2

Using Template driven approach the solution is as followed.

 selectedDate = new Date();
<input class="form-control"
                     matInput
                     [matDatepicker]="dp3"
                     [min]="today"
                     [max]="max"
                     disabled
                     [(ngModel)]="selectedDate"
                     (ngModelChange)="dateUpdated()"
                     name="currentDate"
                   />
                   <mat-datepicker-toggle
                     matSuffix
                     [for]="dp3"
                   ></mat-datepicker-toggle>
                   <mat-datepicker #dp3 disabled="false"></mat-datepicker> ```
SaiSurya
  • 1,046
  • 8
  • 14
1

in your .html----

     <mat-form-field style="width:150px;"  color="accent">
            <mat-label>Choose From Date</mat-label>
            <input  class="example-events" matInput [matDatepicker]="picker1"  [ngModel]="dateF" >
            <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
            <mat-datepicker #picker1 ></mat-datepicker>
     </mat-form-field>

in your .ts----

dateF:any=new Date();
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
ASH ASH
  • 19
  • 2
-2

Here is my answer,

in your .html

<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
    <input [matDatepicker]="picker" matInput placeholder="Date" autocomplete="off" name="date" formControlName="date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
          <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

in your .ts

this.form = this.formBuilder.group({
   date: new FormControl(new Date()), // Current Date
               ...
});

This will set current date as default date.

Dhanika
  • 749
  • 7
  • 12
  • Works this way: new FormControl(new Date()) | BUT doesn't work if I am using it as --> new FormControl({value: new Date()}, [Validators.required]) – Andrei Maimas Jun 14 '19 at 17:52