Home > Software engineering >  How to get month name and year from datetime string
How to get month name and year from datetime string

Time:06-04

I have a json containing field Month having string datetime-

{
 Month : "31-Jan-2022 12:00 AM (EST)"
 ....
 ....
}

How can I fetch Month Name and Year from this string using date functions in javascript?

I need output to be Jan-2022

CodePudding user response:

let str = "31-Jan-2022 12:00 AM (EST)"
let date = new Date(str)
const month = date.toLocaleString('default', { month: 'long' });
const year = date.getFullYear()
console.log(month,year);

  • Related