Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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/1407 and Deprecation warning: moment construction falls back to js Date but neither seems to work on my problem.

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!

like image 635
Deisy Laymi Avatar asked Jan 03 '15 10:01

Deisy Laymi


People also ask

Why is moment JS being deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor. To work around this issue, specify a format for the string being passed to moment() .

Is moment JS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.

What can I use instead of moment in JavaScript?

There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.

How do I get today's date in MomentJS?

To get current date with Moment and JavaScript, we use the moment function without any arguments. const datetime = moment(); to call moment to get a moment object with the current date.


1 Answers

Actually I found the problem.

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

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!

like image 82
Deisy Laymi Avatar answered Nov 15 '22 16:11

Deisy Laymi