Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading