Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  78] [ 6]  / answers: 1 / hits: 21006  / 7 Years ago, tue, march 28, 2017, 12:00:00

I am using moment.js in my TypeScript (Ionic2/Angular2) project. Following on from this post, I now want to try out a plugin moment-duration-format



I have the npm package and the type definition and am able to use by import as..



import moment from 'moment';
...
let duration = moment.duration(decimalHours, 'hours');


I now want to use moment-duration-format



I have installed via npm install moment-duration-format --save and then the type definition via npm i @types/moment-duration-format --save.



I can see both npm modules.



As always, there is always some mystery on how to use such type definitions (the useage including import never seems to be in any doco).



I have tried adding import 'moment-duration-format';, import duration from 'moment-duration-format'; (moment-duration-format/index.d.ts' is not a module.)



I get an error when trying to use as follows..



let dd = moment.duration.format(400.99, 'hours').format('D:HH:mm');

// (TS error [ts] Property 'format' does not exist on type '(inp?: DurationInputArg1, unit?: DurationConstructor) => Duration'.


Does anyone have any idea on how to use this in TypeScript.



Thanks in advance


More From » angular

 Answers
23

I was using the workaround here, but it now looks like the ype def has been fixed. Getting an update of the type def So I can do the following...



import * as moment from 'moment';
import 'moment-duration-format';

let duration = moment.duration(decimalHours, 'hours') ;
let options : moment.DurationFormatSettings = {
forceLength : false,
precision : 0,
template : formatString,
trim : false
};
let result = duration.format(formatString, 0, options);

[#58361] Saturday, March 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;