Home > OS >  Date conversion in js
Date conversion in js

Time:09-11

Is there a shorter way of converting date e.g. "2021-08-19" to "19 Aug 2021" or i need to every time create a new Date object??

const date = "2021-08-19";

const day = new Date(date).getDate()
const month = new Date(date).toLocaleString("en-US", {month: "short",})
const year = new Date(date).getFullYear()

console.log(day, month, year);

CodePudding user response:

You could use UK format with some options for the date.

const
    options = { year: 'numeric', month: 'short', day: 'numeric' },
    date = "2021-08-19";

console.log(new Date(date).toLocaleString("en-UK", options));

  • Related