How to add days to today in day-month-year format? I tried this code but additionally get the time zone and month in the short word name. I want to receive, for example, August 12, 2023 here is the code:
JavaScript
x
9
1
Date.prototype.addDays = function(days) {
2
var date = new Date(this.valueOf());
3
date.setDate(date.getDate() + days);
4
return date;
5
}
6
7
var date = new Date();
8
9
console.log(date.addDays(5));
Advertisement
Answer
To get the format: Month Day, Year
, Simply use ECMAScript Internationalization API:
JavaScript
1
2
1
return date.toLocaleString('en-us',{month:'long', year:'numeric', day:'numeric'})
2
JavaScript
1
4
1
month:'long' //August
2
day:'numeric' //12
3
year:'numeric' //2023
4
Note: ‘long’ uses the full name of the month, ‘short’ for the short name,