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

Converting a date in to a different type of date using javascript

I need to convert a date like this one: Fri Jul 28 2023 00:00:00 GMT+0200 (Central European Summer Time) Into a date like this: 2023-07-26 or viceversa using javascript, is it possible or impossible?

>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

const inputDate = "Fri Jul 28 2023 00:00:00 GMT+0200 (Central European Summer Time)";
const dateObject = new Date(inputDate);
const year = dateObject.getFullYear();
const month = String(dateObject.getMonth() + 1).padStart(2, '0');
const day = String(dateObject.getDate()).padStart(2, '0');
const resultDate = `${year}-${month}-${day}`;

console.log(resultDate); // Output: "2023-07-28"

Reverse:

const inputDate = "2023-07-28";
const dateObject = new Date(inputDate);
const options = { weekday: 'short', month: 'short', day: '2-digit', year: 'numeric', timeZoneName: 'short' };
const resultDate = dateObject.toLocaleString('en-US', options);

console.log(resultDate);
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