Angular 8: How can I set time for angular material datepicker?

4k Views Asked by At

I need to set time for angular material datepicker, as if i select current date it gives me the current time of the system. I want to set the time as 00:00:00. How can i do that?

I am using reactive forms. here is the code.

today = new Date();

createForm() {
  this.newForm = this.fb.group({
    date: this.today
  })
}

html

<div class="input-group">
  <span class="input-group-addon" (click)="dt.open();">
<input [matDatepicker]="dt" type="text" class="input-form input_fullWidth"formControlName="date" fullWidth fieldSize="small">
<div class="datepicker-icon-div">
<i class="fa fa-calendar"></i>
</div>
</span>
  <span>
<mat-datepicker #dt></mat-datepicker>
</span>
</div>

here is the output i am getting

Tue Mar 31 2020 11:33:47 GMT+0530 (India Standard Time)

I want the time should be

Tue Mar 31 2020 00:00:00 GMT+0530 (India Standard Time)

2

There are 2 best solutions below

0
On BEST ANSWER

you can add below way it working

    getDate : any
    createForm() {
      this.newForm = this.fb.group({
        date: this.getDate;
      });

   ngOnInit(){
      const today = new Date();
      const SelectedDate = `${today.getFullYear()}-${today.getMonth() + 1}-${today .getDate()}`;
      this.getDate = new Date(SelectedDate);
      this.createForm();
   }
0
On

You can use this code:

let startDate = new Date(this.yourForm.value.startDate);
let endDate = new Date(this.yourForm.value.endDate);
let startTime = this.yourForm.value.startTime.split(":");
let endTime = this.yourForm.value.endTime.split(":");

startDate.setHours(startTime[0]);
startDate.setMinutes(startTime[1]);
endDate.setHours(endTime[0]);
endDate.setMinutes(endTime[1]);