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

问题描述:

我正在使用角材料日期选择器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