Set default value for angular material datePicker with angular 5
Asked Answered
C

9

28

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],
                   ...
Courcy answered 14/10, 2018 at 4:59 Comment(2)
Do you want to set current date as default date?Nesto
What's this 3888000000 representing?Dudden
F
28

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())
Fubsy answered 18/7, 2019 at 12:39 Comment(3)
this is the best waySkaggs
not anymore :D does not work with Angular 15Dairyman
It works for all angular versions, I tested this on Angular 17, make sure you are importing ReactiveFormsModule in your imports array and you should be fine.Perce
M
19

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

Madalinemadalyn answered 14/10, 2018 at 5:36 Comment(5)
hi! thanks for the answer but it still does not showing :/Courcy
@AlexLungu Not sure about ur case. Did you check the demo stackblitz? It works!Madalinemadalyn
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 misunderstandingThurber
@AlexLungu That's fine. Post a new question if you still facing issue.Madalinemadalyn
working as expectedJudaist
I
9

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>))
Importance answered 10/11, 2019 at 15:4 Comment(1)
While using formControl, you have to import ReactiveFormsModule to your imports array.Positronium
N
3

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>
Nates answered 16/7, 2020 at 20:57 Comment(0)
S
3

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

 birthdayCtrl: ['1999-01-31']
Saladin answered 9/8, 2020 at 4:30 Comment(0)
R
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> ```
Roadster answered 30/12, 2020 at 13:15 Comment(0)
U
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();
Unbidden answered 6/12, 2019 at 9:5 Comment(0)
R
-1

ngModel works for me...

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

<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
   <input matInput [picker]="picker" placeholder="Date"
                  autocomplete="off"
                  name="date" 
                  formControlName="date" [ngModel]="date">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker  [startAt]="startDatePicker" #picker></mat-datepicker>
</mat-form-field>
Riga answered 6/11, 2019 at 14:50 Comment(0)
N
-3

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.

Nesto answered 14/10, 2018 at 5:33 Comment(1)
Works this way: new FormControl(new Date()) | BUT doesn't work if I am using it as --> new FormControl({value: new Date()}, [Validators.required])Xanthin

© 2022 - 2024 — McMap. All rights reserved.