【问题标题】:momentJS toNow() and to() to display actual times as opposed to 1 hour or 44 minutesmomentJS toNow() 和 to() 显示实际时间,而不是 1 小时或 44 分钟
【发布时间】:2018-08-17 15:51:57
【问题描述】:

如何让 momentJS 正确显示内容?例如

runTime = moment(info.timestamp).toNow(true)

runTime = moment(info.timestamp).to(info.timestamp + info.duration, true);

正在显示类似的东西

23 分钟

1 小时

info.timestamp 处于纪元中。 我只想让它显示小时:分钟:秒

【问题讨论】:

  • 哪个语句?
  • 我希望这两个语句都这样返回。

标签: javascript node.js reactjs momentjs


【解决方案1】:

如果您想要自定义显示,您可能想要使用difference。类似的东西

var t1=moment(info.timestamp);
var t2=moment(info.timestamp+info.duration);
var now=moment();

var diff1=moment(now.diff(t1)).format('HH:mm:ss');
var diff2=moment(t2.diff(t1)).format('HH:mm:ss');

【讨论】:

  • 第二行代码效果很好,但是替换 toNow() 怎么样
  • 你会做类似moment(moment(info.timestamp).diff(moment())).format('HH:mm:ss')的事情。使用moment()获取当前时间
  • 谢谢!我仍在学习关于 momentJS 的方法,这是一个巨大的帮助
猜你喜欢
  • 2020-09-08
  • 2021-08-31
  • 2015-01-25
  • 2021-07-10
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
相关资源
最近更新 更多