How to convert date into ISO date string

 function getISODateString(dateString) {

            try {

                const [day, month, year] = dateString.split(‘/’);

                const date = new Date(`${year}${month}${day}`);

                if (isNaN(date)) {

                    throw new Error(“Invalid date”);

                }

                return date.toISOString();

            } catch (error) {

                log.error(“Invalid date string:”, dateString, error);

                return null;

            }

        }

       

pass the date string into the function and the split the date and change into the format ‘YYYY-MM-DD’ and check if it is a validate date. If it is not a valid date, then will throw an error. the return date in the ISOString date format using “toISOString()”.

Leave a comment

Your email address will not be published. Required fields are marked *