在 Angular 中将毫秒转换为 HH:mm:ss 格式

Fig*_*Fig 4 html formatting datetime angular

我正在尝试将毫秒转换为可读的小时、分钟、秒格式。我尝试过手动执行此操作,但我陷入困境,我觉得应该有一种更简单的方法。

当前手写秒表:

ngOnInit(){
    //alert(this.datePipe.transform(new Date()));

    let timer = Observable.timer(0, 1000);
    timer.subscribe(t=>{
        this.today = this.datePipe.transform(t);
        this.second = t;
        if(this.second==60){
            this.second = 0;
            this.minute += 1;
        }else if(this.minute == 60){
            this.minute = 0;
            this.hour += 1;
        }

    });
}
Run Code Online (Sandbox Code Playgroud)

一旦 t 超过 60 秒,秒数将不会重置,并且将继续超过 60。

所以我的问题是,有没有一种更简单、更有效的方法?我读过这篇文章,但不太明白它在我的示例DatePipe中是如何工作的

超文本标记语言

<div class="client-time">
    <span>Client time</span>
    <br/>
    <strong>{{hour}}:{{minute}}:{{second}}</strong>    
  </div>
</footer>
Run Code Online (Sandbox Code Playgroud)

基本上我试图显示用户在页面上停留的时间。

Bra*_*ley 6

要将 DatePipe 与时间结合使用,请查看以下内容:

  start = new Date(0,0,0);

  ngOnInit() {
    Observable.interval(1000)
      .startWith(0)
      .subscribe(t => {
        this.start = new Date(0, 0, 0);
        this.start.setSeconds(t);
      });
  }
Run Code Online (Sandbox Code Playgroud)

HTML 应该是

{{start | date:'HH:mm:ss'}}
Run Code Online (Sandbox Code Playgroud)