Advertisements
How can I get a MySQL datetime stamp converted to a month using JavaScript?
MySQL date : 2021-12-14 10:15:56
output : December
using JavaScript code
I tried this
const moonLanding = new Date(‘2021-12-14 10:15:56’);
// const moonLanding = new Date(‘July 20, 69 00:20:18’);
console.log(moonLanding.getMonth());
output is > 11
>Solution :
You can use toLocaleString() method like this:
const timestamp = '2021-12-14 10:15:56';
const date = new Date(timestamp);
const monthName = date.toLocaleString('en-US', { month: 'long' });
console.log(monthName);