adsense

Sunday, July 10, 2022

JavaScript format Date ( DD/MM/YYYY)

 Following function will check if input date is entered in DD/MM/YYYY format,  It also validates a minimum date that can be entered to an input field (original code from here)


         function IsValidDate(inputDate) {

            const regex = /^\d{2}\/\d{2}\/\d{4}$/;

            var minDate = new Date("01/01/1900 12:00:00");


            if (inputDate.match(regex) === null) {

                return false;

            }


            const [day, month, year] = inputDate.split('/');

            const isoFormattedStr = `${year}-${month}-${day}`;

            const date = new Date(isoFormattedStr);

            const timestamp = date.getTime();


            if (typeof timestamp !== 'number' || Number.isNaN(timestamp)) {

                return false;

            }


            if (isoFormattedStr < minDate.toISOString()) {

                return false;

            }


            return date.toISOString().startsWith(isoFormattedStr);

        }

Cheers
Samitha


No comments:

Post a Comment