I have a date like 2019-02-01. how can I change it to February 2019?
CodePudding user response:
You can just do this:
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date("2019/02/01");
const dateString = `${monthNames[d.getMonth()]} ${d.getFullYear()}`;
console.log(dateString);
This will return the month in English, no matter the system's language.
Note that you should use new Date("2019/02/01")
(with slashes) to specify a date in your timezone. If you use new Date("2019-02-01")
, then it will assume UTC time, and weird stuff will happen (see this answer for more info on this).
CodePudding user response:
const d = new Date("2019/02/01");
const m = d.toLocaleString('default', { month: 'long' });
console.log(m, d.getFullYear());
CodePudding user response:
/* you can use the following code to convert date to the required format. */ var todaysDate = new Date(); var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var formatted_date = months[todaysDate.getMonth()] " " todaysDate.getDate() " " todaysDate.getFullYear();