javascript 在 Moment.js 中,为什么不减去“周”工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20664319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:59:49  来源:igfitidea点击:

In Moment.js, why doesn't subtract by 'week' work?

javascriptmomentjs

提问by Dave Fontenot

I'm using Moment.jsand following line of code doesn't seem to actually do anything:

我正在使用Moment.js并且以下代码行似乎实际上并没有做任何事情:

moment().subtract('week', 1)

In the documentation, it shows an example of subtract being used with 'days'but it doesn't specify that it's the only string you can use. In most of the other functions, it's an option where you can use 'days', 'weeks', 'months', so I'm not sure if I'm doing something wrong or 'weeks'just isn't supported: Moment.js Subtract Documentation

在文档中,它显示了一个使用减法的示例,'days'但它没有指定它是您可以使用的唯一字符串。在大多数其他功能中,您可以选择使用'days', 'weeks', 'months',所以我不确定我是做错了什么还是'weeks'不支持: Moment.js Subtract Documentation

Here's the example of subtracting days:

这是减去天数的示例:

moment().subtract('days', 7);

It's also what I ended up using instead of 'weeks'but I'm still curious why 'weeks'aren't supported.

这也是我最终使用的而不是,'weeks'但我仍然很好奇为什么'weeks'不支持。

回答by jemiloii

You have it backwards from the Moment API.

您可以从 Moment API 逆向使用它。

moment().subtract(1, 'week');

回答by Chasky

This is a bit old question but it appears that even back then it was relevant.

这是一个有点老的问题,但似乎即使在当时也是相关的。

Definetely nowadays is even more relevant to consider parse.com moment.js version (1.7.2) to the current moment.js version (2.8.4) and the only API doc that parse.com makes reference to

现在更重要的是考虑 parse.com moment.js 版本(1.7.2)与当前的 moment.js 版本(2.8.4)以及 parse.com 引用的唯一 API 文档

Check an answer to this in a previous post in Trouble using the Moment module

使用 Moment 模块的 Trouble中的上一篇文章中检查对此的答案

I would suggest to work always with latest moment.js so you can properly work with the provided documentation (during my search I was not able to find 1.7.2 documentation.. beside you will miss many great features working with the version provided in parse.com.

我建议始终使用最新的 moment.js,以便您可以正确使用提供的文档(在我的搜索过程中,我无法找到 1.7.2 文档......此外,您将错过许多与 parse 中提供的版本一起使用的很棒的功能.com。

Add the new version as indicated in the same post in the accepted answer.

在已接受的答案中添加同一帖子中指示的新版本。