29

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],
                   ...
2
  • Do you want to set current date as default date? Commented Oct 15, 2018 at 3:45
  • What's this 3888000000 representing? Commented Aug 8, 2019 at 3:27

9 Answers 9

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())
Sign up to request clarification or add additional context in comments.

3 Comments

this is the best way
not anymore :D does not work with Angular 15
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.
21

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

5 Comments

hi! thanks for the answer but it still does not showing :/
@AlexLungu Not sure about ur case. Did you check the demo stackblitz? It works!
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
@AlexLungu That's fine. Post a new question if you still facing issue.
working as expected
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>))

1 Comment

While using formControl, you have to import ReactiveFormsModule to your imports array.
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>

Comments

3

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

 birthdayCtrl: ['1999-01-31']

Comments

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> ```

Comments

0

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();

Comments

-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>

Comments

-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.

1 Comment

Works this way: new FormControl(new Date()) | BUT doesn't work if I am using it as --> new FormControl({value: new Date()}, [Validators.required])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.