urish / ngx-moment

moment.js pipes for Angular
MIT License
1.17k stars 154 forks source link

How to use momentjs in code #158

Open artemik opened 7 years ago

artemik commented 7 years ago

How to use momentjs just in code? Not in templates.

tplk commented 7 years ago

All pipes implement PipeTransform which has method transform(value: any, ...args: any[]). You can use it like this:

let a = new Date();
let b = (new DateFormatPipe()).transform(a, 'YYYY-MM-DD HH:mm');
console.log(a);
console.log(b);

Will output

Wed Aug 09 2017 19:52:18 GMT+0300 (Russia TZ 2 Standard Time)
2017-08-09 19:52

Also instead of creating a new instance you can provide pipe in parent module and inject in constructor (just like a service).

Don't forget to:

import { DateFormatPipe } from 'angular2-moment';
jcbowyer commented 6 years ago

How do you amUtc from code?