Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amDuration with moment object #181

Open
alobban opened this issue Nov 1, 2017 · 4 comments
Open

amDuration with moment object #181

alobban opened this issue Nov 1, 2017 · 4 comments

Comments

@alobban
Copy link

alobban commented Nov 1, 2017

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:

  • Angular version? 4.3.4
  • TypeScript version? 2.2.0
  • moment version? 2.18.1
  • Are you using moment-timezone? 0.5.13
  • Are you using the angular-cli? 1.4.4
  • Using Rollup/Webpack/System.js/Ionic/similar? Webpack
@louisl
Copy link

louisl commented Aug 20, 2018

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

@StickNitro
Copy link

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
Copy link

ben12 commented Apr 18, 2019

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
Copy link

ben12 commented Apr 18, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants