Skip to content

Instantly share code, notes, and snippets.

@qdouble
Created January 16, 2016 17:04
Show Gist options
  • Save qdouble/2c6db517b11c95b9d1bd to your computer and use it in GitHub Desktop.
Save qdouble/2c6db517b11c95b9d1bd to your computer and use it in GitHub Desktop.
Moment Replacement for Date Pipe
import {Pipe, PipeTransform} from 'angular2/core';
import {isBlank, isPresent} from 'angular2/src/facade/lang';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import * as moment from 'moment';
@Pipe({name: 'moment'})
export class MomentPipe implements PipeTransform {
static _ALIASES: {[key: string]: String} = {
'medium': 'MMM D YYYY, h:mm:ss A',
'short': 'M/D/YY, h:mm:ss A',
'fullDate': 'dddd, MMMM Do YYYY, h:mm:ss A',
'longDate': 'MMMM Do, YYYY',
'mediumDate': 'MMM D, YYYY',
'shortDate': 'M/D/YY',
'mediumTime': 'HH:mm:ss A',
'shortTime': 'HH:mm A'
};
transform(value: string, args: any[]): any {
if (isBlank(value)) return null;
var pattern: string = isPresent(args[0]) && args[0].length > 0 ? args[0] : 'medium';
pattern = <string>StringMapWrapper.get(MomentPipe._ALIASES, pattern);
let temp = moment(new Date(value));
return moment(temp).format(pattern);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment