Adrian
Adrian

Reputation: 71

Date conversion in js

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);

Upvotes: 0

Views: 38

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386816

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));

Upvotes: 1

Related Questions