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


Sunday, July 3, 2022

setInterval() and setTimeout()

If we want to execute a specific JS function after some time there are two options to use fro,.  


  • setTimeout used  run a function once after the interval specified 
  • setInterval used   run a function repeatedly, after the interval specified and repeating continuously at that interval.

If we want to stop the indefinite function call  we can use clearInterval with setInterval.


Cheers,

Samitha