【问题标题】:Moment.js: How to get general semantic expression of time given a number of daysMoment.js:如何在给定天数的情况下获得时间的一般语义表达
【发布时间】:2016-10-18 05:21:35
【问题描述】:

假设我有一个字段,其中包含以天数表示的妊娠期:

var gestation = 132; // days

现在,假设使用 Moment.js,我想返回一个值,该值根据其长度计算出该妊娠期的语义表达。换句话说,如果妊娠期是 10 天,我希望该方法返回 about a week10 days 或类似的值。如果妊娠期是 365 天,我希望方法返回值 a year 等等。

如何编写 Moment.js 表达式来实现这一点?

This seems similar to the Time to X approach described here in the docs。但不完全是。

【问题讨论】:

  • 您能告诉我们到目前为止您已经尝试并取得了哪些成就?

标签: javascript time momentjs


【解决方案1】:

一种可能的解决方案是将subtract()to() 结合使用,就像在这个jsfiddle 中一样

【讨论】:

    【解决方案2】:

    使用humanize() 方法。

    moment.duration(gestation, 'days').humanize();
    

    Found in the documentation here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      相关资源
      最近更新 更多