using moment.js package in ionic 2 project

Thanks @mosca90 you got me 90% of the way!

I used your info to create a custom pipe, which solved my issues completely. Here's the source of the pipe:

import { Pipe, PipeTransform } from '@angular/core';
import moment from 'moment';

/**
 * Generated class for the MomentjsPipe pipe.
 *
 * See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
 * Angular Pipes.
 */
@Pipe({
  name: 'momentjs',
})
export class MomentjsPipe implements PipeTransform {
  /**
   * Takes a date value and returns a pretty string from current time, 
   * for instance: "four hours ago" or "in eleven minutes".
   */
  transform(value: string, ...args) {
    return moment(value).fromNow();
    //return value.toLowerCase();
  }
}

So now I can just do:

<div>{{ date | momentjs }}</div>

and I see:

ten minutes ago.

Perfection! Thank you again!


Check this link for typescript.

1 - Install via NPM:

npm install moment -S

2 - Import in your Typescript file:

import moment from 'moment';

3 - Use in your Typescript file:

let data = moment().format('YYYYMMDD');
let time = moment().format('HHmmss');
console.log('today is: ', data + ' and time: ', time);

I hope to have been a help to you. :)

EDIT: Still works fine with Ionic v3.9.2 (2018-03-08)

Tags:

Angular

Ionic2