简体   繁体   中英

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

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)

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

you can use this code

 let startDate = new Date(this.yourForm.value.startDate); let endDate = new Date(this.yourForm.value.startDate); 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]);

let endDate = new Date(this.yourForm.value.startDate);
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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM