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

时间:2018-05-28 09:40:16

标签: javascript angular typescript angular-material angular5

我正在使用角材料datepicker https://material.angular.io/components/select/overview

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

我有什么方法可以从中获得当前时间吗?

4 个答案:

答案 0 :(得分:1)

您可以使用 ngModelChange 解析日期,然后再将其设置为您的模型,我建议您使用momentJS进行日期操作。

HTML中的

DispatchQueue.main.async

在你的Component.ts

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

您可以在https://stackblitz.com/edit/angular-ecq2lc

找到完整的解决方案

答案 1 :(得分:0)

角度材料现在没有提供时间你需要手动从timeStamp获取时间,试试这个 -

ng serve --aot

答案 2 :(得分:0)

现在物料日期选择器只提供当前日期(没有当前时间),但官方仓库中存在未解决的问题,因此我们可能会在不久的将来看到时间选择器。

答案 3 :(得分:0)

        For Example currenttime = Mon May 28 2018 00:00:00 GMT+0530 (IST)

    Currenttime is an example if  u using current date means just use new Date()  

    //Sample function calling Method 
            Exacttime(){
            this.functionName = this.diffhours(new(currenttime))
            }

            diffhours(currenttime){
               var diff = new Date(currenttime);
                diff .getHours(); // => 9
                diff .getMinutes(); // =>  30
                diff .getSeconds(); // => 51

}