0

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

2
  • momentjs may satisfy you Commented Sep 10, 2022 at 13:25
  • 1
    momentjs.com/docs/#/-project-status no, even momentjs says you don't need momentjs anymore. Commented Sep 10, 2022 at 13:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.