urish / ngx-moment

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

amDuration with moment object #181

Open alobban opened 7 years ago

alobban commented 7 years ago

Description of the Issue and Steps to Reproduce:

Did you search for duplicate issue? [Yes / No] YES Please describe the issue and steps to reproduce, preferably with a code sample / plunker: I tried piping a moment object with amDuration: 'seconds' based on the illustrations, I expect the string display to say '4 minutes' when the object is 4 minutes ahead of time.

I keep receiving 'a few seconds'

Ensure your issue is isolated to angular2-moment. Issues involving third party tools will be closed unless submitted by the tool's author/maintainer.

Environment: Webstorm in Windows 10; angular2-moment 1.7.0 Please answer the following questions:

louisl commented 6 years ago

You are probably passing it a string "4" instead of numeric 4. That was my problem.

StickNitro commented 6 years ago

If you are passing amDuration a value in minutes then you need to tell amDuration that the value is minutes, e.g. 4 | amDuration:'minutes' will output 4 minutes, if you do intend to use seconds then you would be piping a value in seconds 240 | amDuration:'seconds' will output 4 minutes

ben12 commented 5 years ago

Same issue.

My model contains a Duration instance. amDuration pipe does not work.

duration = moment.duration('P3M4D');
{{ duration | amLocale: 'fr' | amDuration: 'days' }}

display 'a few seconds', and it is not the good locale.

We can not display instance of moment.Duration with amDuration ? amDuration seems to be more a parser than a formatter. So, how to do ?

ben12 commented 5 years ago

Solution may be to accept no argument like this:

  transform(value: any, ...args: string[]): string {
    if (typeof args === 'undefined' || args.length !== 1) {
      // throw new Error('DurationPipe: missing required time unit argument');
      return moment.duration(value).humanize();
    }
    return moment.duration(value, args[0] as moment.unitOfTime.DurationConstructor).humanize();
  }

But it not solve amLocale problem. It seems to accept only date, but duration.locale('fr') is possible.