如何从角度材料日期选择器获取当前时间? [英] How to I get current time from angular material Date picker?

查看:26
本文介绍了如何从角度材料日期选择器获取当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于如何从角度材料日期选择器获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆