[Solved] Last day of the previous month – In Specific format using JS [duplicate]


I always use Moment.js whenever I want work with dates which gives you lot’s of options for format your date, but since you said with plain javascript, you can made a method like this to format your date :

function formatDate(date) {
   date.setDate(0);
   var monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
 ];

 var day = date.getDate();
 var monthIndex = date.getMonth();
 var year = date.getFullYear();

 return  monthNames[monthIndex] +  ' ' + day  + ' ' + year;
}

console.log(formatDate(new Date())); 

Basiclly date.setDate(0); will change the date to last day of the previous month.

2

solved Last day of the previous month – In Specific format using JS [duplicate]