如何从角形材料日期选择器中获取当前时间?

问题描述:

我正在使用角形材料日期选择器 https://material.angular.io/components/select/overview

I am using angular material datepicker https://material.angular.io/components/select/overview

,但这仅返回日期,而不返回当前时间:2018年5月28日星期一00:00:00 GMT + 0530(IST)

but this returns only the date and not the current time : Mon May 28 2018 00:00:00 GMT+0530 (IST)

有什么办法可以从中获取当前时间吗?

Is there any way I can get the current time also from this?

您可以使用 ngModelChange 解析日期,然后再将其设置为模型,我建议您使用momentJS以便于日期操作.

You can use the ngModelChange to parse the date before set it to your model, i recommend you momentJS for easy date manipulations.

在HTML中

 <input [ngModel]="date" (ngModelChange)="onDataChange($event)"  matInput [matDatepicker]="picker" placeholder="Choose a date">

在您的Component.ts中

In your Component.ts

  onDataChange(newdate) {
    const _ = moment();
    const date = moment(newdate).add({hours: _.hour(), minutes:_.minute() , seconds:_.second()})
    this.date = date.toDate();
    console.log({hours: _.hour(), minutes:_.minute() , seconds:_.second()})
  }

您可以在此处找到完整的解决方案 https://stackblitz.com/edit/angular-ecq2lc

you can find the full solution here https://stackblitz.com/edit/angular-ecq2lc