skip to Main Content

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

2

Answers


  1. This seems correct.
    December is the 11th month, if you consider a zero based index.
    According to the documentation, January is month 0, which would make December month 11:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth

    Login or Signup to reply.
  2. 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); 
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search