using moment.js package in ionic 2 project

前端 未结 2 923
不思量自难忘°
不思量自难忘° 2020-12-30 05:15

i want to use in Moment.js package for dates in my project ionic 2 and i\'m not sure how to do that this is the link moment js link

i have datetime variable and i w

2条回答
  •  渐次进展
    2020-12-30 06:11

    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:

    {{ date | momentjs }}

    and I see:

    ten minutes ago.
    

    Perfection! Thank you again!

提交回复
热议问题