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