Skip to content

Instantly share code, notes, and snippets.

@saplaum
Forked from bostjanpisler/moment.pipe.ts
Created June 24, 2016 10:14
Show Gist options
  • Save saplaum/68cad7afdcef71922eb0ade55eef9fea to your computer and use it in GitHub Desktop.
Save saplaum/68cad7afdcef71922eb0ade55eef9fea to your computer and use it in GitHub Desktop.
angular 2 moment pipe
import {Pipe, PipeTransform} from 'angular2/core';
import * as moment from 'moment';
/*
* Time helper using momentjs
* Usage:
* timestamp | moment:'DD.MM.YYYY'
* Defaults to 'L' - locale ie. '01/24/2016'
*/
@Pipe({name: 'moment'})
export class MomentPipe implements PipeTransform {
transform(value:number, args:string[]) : any {
let date = moment(value);
if (date.isValid()) {
return date.format(args[0] || 'L');
} else {
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment