简体   繁体   English

在几周而不是几天的时间内从moment.js diff / from / fromNow获取结果

[英]Getting result from moment.js diff/from/fromNow in weeks instead of days

Given this code: 给出以下代码:

var now = moment();
now.day("Tuesday");
var displayed_week_day = moment();
var one_week = moment.duration(1, 'weeks');
displayed_week_day.day("Tuesday");
displayed_week_day.add(one_week);
console.log(displayed_week_day.from(now));
displayed_week_day.add(one_week);
console.log(displayed_week_day.from(now));

The output is: 输出为:

in 7 days
in 14 days

The docs would lead me to believe that this would output as "in 1 week" or "in 2 weeks". 这些文档会让我相信这将输出为“ 1周内”或“ 2周内”。 I don't see a way to pass an argument to this to make it do that. 我没有办法将参数传递给它来做到这一点。

There was an earlier stack question here: How to get duration in weeks with Moment.js? 这里有一个较早的堆栈问题: 如何使用Moment.js在几周内获得持续时间? but this response is quite old and does not seem to apply to current versions of moment.js. 但是此响应很旧,似乎不适用于最新版本的moment.js。 Does anyone have thoughts on how to change the output format of this? 有没有人想过如何更改其输出格式?

Actually, in the docs it says that anywhere from 22 hours to 25 days is represented in terms of days. 实际上,在文档中它表示以天为单位表示从22小时到25天不等。 (>=22 && < 36 hours rounds to a single day, >= 36 hours rounds to 2 days) (> = 22 && <36小时四舍五入为一天,> = 36小时四舍五入为2天)

See the chart under the Time from now section. 请参阅从现在开始时间部分下的图表。

The ability to customized these ranges has been requested and is an open issue here . 要求能够自定义这些范围的功能,这是一个未解决的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM