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

11,339

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!

Share:
11,339
Deisy Laymi
Author by

Deisy Laymi

Updated on July 24, 2022

Comments

  • Deisy Laymi
    Deisy Laymi almost 2 years

    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!

  • Deisy Laymi
    Deisy Laymi over 9 years
    Thanks for the tip! Edited.
  • arjun kori
    arjun kori about 8 years
    that was very good answer
  • murli2308
    murli2308 about 8 years
  • Ben
    Ben almost 8 years
    The comment thread in the issue has a pretty good explanation as well, now: github.com/moment/moment/issues/1407
  • Panayiotis Georgiou
    Panayiotis Georgiou over 3 years
    Moment’s usage is so widespread that it’s impossible to deprecate the current version over time. Check out this [post][1] on alternative options Migrating away from moment.js panayiotisgeorgiou.net/migrating-away-from-moment-js