momentjsmoment-timezone

How to get year and month based on count using moment


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


Solution

  • 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');