saaadel / angular-moment-timezone

moment-timezone.js pipes for Angular
MIT License
5 stars 3 forks source link

How to use moment inside template? #1

Closed Abhivendra closed 7 years ago

Abhivendra commented 7 years ago

I want to use moment.fromNow() in the angular material table in my template. How to do it?

saaadel commented 7 years ago

This repo is for timezone shifts only. I think you need ask it in https://github.com/urish/angular2-moment - There's everything about moment namespace. Anyway, I see there are no solution for you right now. You can write your own pipe for it by yourself.

Something like following code:

import {Pipe, PipeTransform} from "@angular/core";
import * as moment from "moment-timezone";

@Pipe({
  name: 'amFromNow'
})
export class FromNowPipe implements PipeTransform {

  transform(value: moment.MomentInput, withoutSuffix: boolean = false): string {
    return moment(value).fromNow(!!withoutSuffix);
  }
}

How to use:

<span>{{ momentValue | amFromNow }} </span> <!-- With 'ago' suffix -->
<span>{{ momentValue | amFromNow:true }} </span> <!-- Without it -->