如何在TypeScript中使用矩持续时间格式? [英] How to use moment-duration-format in TypeScript?

查看:128
本文介绍了如何在TypeScript中使用矩持续时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TypeScript(Ionic2/Angular2)项目中使用moment.js.从帖子,我现在想尝试一个插件 moment-duration-format

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

我有npm包和类型定义,并且可以通过import as使用.

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');

我现在要使用瞬间-持续时间格式

我已经通过npm install moment-duration-format --save安装了,然后通过npm i @types/moment-duration-format --save进行了类型定义.

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

我可以看到两个npm模块.

I can see both npm modules.

一如既往,如何使用此类类型定义始终存在一些谜团(包括import在内的用法似乎从未出现在任何doco中).

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).

我尝试添加import 'moment-duration-format';import duration from 'moment-duration-format';(一刻持续时间格式/index.d.ts'不是模块.)

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'.

有人对在 TypeScript 中如何使用它有任何想法.

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

预先感谢

推荐答案

我正在使用

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);

这篇关于如何在TypeScript中使用矩持续时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆