如何使用 ng-forms 显示当前时间

How to display current time using ng-forms

使用ng-forms

显示当前时间
<input
  type="time"
  class="form-control"
  path="time"
  formControlName="time"
  [ngClass]="{'is-invalid': submitted && f.time.errors}"
/>

也试试这个

您可以使用 Angular2 日期管道显示 JavaScript 日期对象。检查文档(链接)以获取您正在寻找的正确格式。

解决方案 1 :

@Component({
  selector: 'date-pipe',
  template: `<div>
    <p>Today is {{today | date}}</p>
    <p>Or if you prefer, {{today | date:'fullDate'}}</p>
    <p>The time is {{today | date:'jmZ'}}</p>
    <p>Finally the date and time is {{today | date:'short'}}</p>
  </div>`
})
export class DatePipeComponent {
  today: number = Date.now();
}

解决方案 2 :

{{date  | date:'yyyy-MM-dd'}}

解决方案 3 :

import { DatePipe } from '@angular/common'
...
constructor(public datepipe: DatePipe){}
...
myFunction(){
 this.date=new Date();
 let latest_date =this.datepipe.transform(this.date, 'yyyy-MM-dd');
}