You have a timezone issue
Also you are wasting moment by not using it to add the month
Note that this date format makes moment complain: '01-Feb-2023'
var x = 3;
var currentDate = moment('01-Feb-2023'); // not a great value for moment
var lastDate = moment(currentDate).add(x, 'M').format("DD-MMM-YYYY");
console.log(lastDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Better: Strict mode is set by passing true as the third parameter to the moment function.
moment('02/01/2023', 'MM/DD/YYYY', true);
var x = 3;
var currentDate = moment('02/01/2023', 'MM/DD/YYYY', true); // better
var lastDate = moment(currentDate).add(x, 'M').format("DD-MMM-YYYY");
console.log(lastDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Without moment - note the new Date(dateString);
is STILL not a good idea with "01-Feb-2023"
due to Why does Date.parse give incorrect results?
But if you can live with this, then here is a version
const dateTimeFormat = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
const addToDate = (dateString, offset) => {
const currentDate = new Date(dateString); // using the Date.parse. Moment warned it would use this
currentDate.setHours(15, 0, 0, 0) // normalise
currentDate.setMonth(currentDate.getMonth() + offset); // add the month
const [month, sp1, day, comma, year] = dateTimeFormat.formatToParts(currentDate);
return `${day.value}-${month.value}-${year.value}`; // There is no DD-MMM-YYYY in INTL so we make our own
}
let x = 3;
const lastDate = addToDate("01-Feb-2023", x);
console.log("01-Feb-2023 plus",x,"months:",lastDate)