Angular 8:如何为 Angular Material 日期选择器设置时间?

我需要为角度材料日期选择器设置时间,就好像我选择当前日期一样,它会给我系统的当前时间。我想将时间设置为 00:00:00。我怎样才能做到这一点?


我正在使用反应形式。这是代码。


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>

这是我得到的输出

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

我想要的时间应该是

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


慕田峪7331174
浏览 126回答 2
2回答

德玛西亚99

您可以添加以下工作方式&nbsp; &nbsp; getDate : any&nbsp; &nbsp; createForm() {&nbsp; &nbsp; &nbsp; this.newForm = this.fb.group({&nbsp; &nbsp; &nbsp; &nbsp; date: this.getDate;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;ngOnInit(){&nbsp; &nbsp; &nbsp; const today = new Date();&nbsp; &nbsp; &nbsp; const SelectedDate = `${today.getFullYear()}-${today.getMonth() + 1}-${today .getDate()}`;&nbsp; &nbsp; &nbsp; this.getDate = new Date(SelectedDate);&nbsp; &nbsp; &nbsp; this.createForm();&nbsp; &nbsp;}

暮色呼如

您可以使用此代码: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]);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5