Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make pipe refresh it self in angular

I have static timestamp from backend and I want to refresh the pipe every 1 second to get date from now and this my pipe

    import { Pipe, PipeTransform } from '@angular/core';
import moment from 'moment';
@Pipe({
  name: 'dateFormat'
})
export class DateFormatPipe implements PipeTransform {

  transform(date: string): string {

    return moment.utc(date, 'X').local().format('DD-MM-YYYY h:mm:ss a');
  }

}

in this case it just convert time stamp to this format , I want to keep date updated how can I do this ?

like image 243
mustafa abdelbadea Avatar asked Oct 27 '25 04:10

mustafa abdelbadea


1 Answers

You should return an observable and use asyncPipe as well.

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
import { interval, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Pipe({
  name: 'dateFormat'
})
export class DateFormatPipe implements PipeTransform {

  transform(date: string): Observable<string> {
    return interval(1000).pipe(map(() => { return moment().format('DD-MM-YYYY h:mm:ss a') }));
  }

}

And you should use it like: {{ 'param' | dateFormat | async}}

like image 109
msleiman Avatar answered Oct 29 '25 20:10

msleiman