如何在 TypeScript 中使用时刻持续时间格式?

2024-04-27

我在 TypeScript (Ionic2/Angular2) 项目中使用 moment.js。继从这个帖子 https://stackoverflow.com/questions/43042337/how-to-format-decimal-hours-greater-than-24-hours-into-hours-minutes-in-javascri,我现在想尝试一个插件时刻-持续时间-格式 https://github.com/jsmreese/moment-duration-format

我有 npm 包和类型定义,并且能够通过 import as..

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

我现在想用时刻-持续时间-格式 https://github.com/jsmreese/moment-duration-format

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

我可以看到两个 npm 模块。

与往常一样,如何使用此类类型定义总是存在一些谜团(包括 import 在内的用法似乎从未出现在任何 doco 中)。

我尝试过添加import 'moment-duration-format';, import duration from 'moment-duration-format';(moment-duration-format/index.d.ts' 不是一个模块。)

尝试使用时出现错误,如下所示。

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

有谁知道如何使用它打字稿.

提前致谢


我正在使用解决方法here https://stackoverflow.com/questions/41383393/moment-duration-format-d-ts-definition-not-extending-moment-module/41412055#41412055,但现在看起来 的类型已经被修复了。获取类型 def 的更新所以我可以执行以下操作......

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 TypeScript 中使用时刻持续时间格式? 的相关文章

随机推荐