javascript Moment JS:弃用警告:moment 构造回退到 js 日期。这是不鼓励的,将在即将发布的主要版本中删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27753778/
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-28 07:56:40  来源:igfitidea点击:

Moment JS: Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release

javascriptnode.jsmomentjs

提问by Deisy Laymi

I'm getting Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.But I'm a newbie I cannot figure out how to fix it so the above message disappear. I think the problem lies in these two lines but I'm not sure.

我得到了Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.但我是一个新手我不知道如何解决它所以上面的消息消失了。我认为问题在于这两行,但我不确定。

var nextMonth = moment(moment(year + "-" + month + "-1")).add(1, "months").format("MM");
var nextYear  = moment(moment(year + "-" + month + "-1")).add(1, "months").format("YYYY");

I have already checked https://github.com/moment/moment/issues/1407and Deprecation warning: moment construction falls back to js Datebut neither seems to work on my problem.

我已经检查了https://github.com/moment/moment/issues/1407弃用警告:moment 构造回退到 js Date但似乎都没有解决我的问题。

I would like to know where in this calculation I should tell the format to the moment or at least how to make this calculations in the right format so the warning disappears.

我想知道在这个计算中我应该告诉现在的格式,或者至少如何以正确的格式进行计算,以便警告消失。

Thanks in advance!

提前致谢!

回答by Deisy Laymi

Actually I found the problem.

其实我发现了问题。

By just adding new Date() to both calculations, it normalized again.

通过将 new Date() 添加到两个计算中,它再次标准化。

var nextMonth = moment(new Date(year, month - 1, 1)).add(1, "months").format("MM");
var nextYear  = moment(new Date(year, month - 1, 1)).add(1, "months").format("YYYY");

I hope it help others!

我希望它能帮助别人!