0

I got obj from DB like {month : 2 , year : 2023}

I need to convert into date based on this obj m

moment({year : 2023 , month:2}).format("MMM YY"))

OUTPUT : Mar 2023

Expected : Feb 2023

How can I do this using moment

  • 1
    see: [Why does the month argument range from 0 to 11 in JavaScript's Date constructor?](https://stackoverflow.com/questions/2552483/why-does-the-month-argument-range-from-0-to-11-in-javascripts-date-constructor) AND the docs of moment.js, https://momentjs.com/docs/#/get-set/month/ – Luuk Jun 10 '23 at 07:33
  • It's have to be between 0 and 11. Just add one to the month every time. For more info; https://momentjscom.readthedocs.io/en/latest/moment/02-get-set/12-month/ – doneforaiur Jun 10 '23 at 07:35
  • Does this answer your question? [Why does the month argument range from 0 to 11 in JavaScript's Date constructor?](https://stackoverflow.com/questions/2552483/why-does-the-month-argument-range-from-0-to-11-in-javascripts-date-constructor) – Luuk Jun 10 '23 at 07:36

1 Answers1

1

According to Moment.js documentation, 'month' uses an array and the indices of an array are zero-based

moment({year : 2023 , month:2}).subtract(1, 'months').format('MMM YY');
msila
  • 26
  • 2