Quantcast
Viewing latest article 47
Browse Latest Browse All 58

Answer by Hoyeon Kim for Detecting an "invalid date" Date instance in JavaScript

Here I came up with a solution that might be helpful for those looking for a test function that can check whether it's given yyyy/mm/dd or mm/dd/yyyy also with serveral symbols such as '/', '-', '.'.

function isValidDate(dateString) {    // Regular expression pattern for mm/dd/yyyy format    const regTestUsa = /^(0?[1-9]|1[0-2])[\/.-](0?[1-9]|1\d|2\d|3[01])[\/.-](\d{2}|\d{4})$/;    // Regular expression pattern for yyyy/mm/dd format    const regTestUNiv = /^(\d{2}|\d{4})[\/.-](0?[1-9]|1[0-2])[\/.-](0?[1-9]|1\d|2\d|3[01])$/;    const regTestYear = /^(\d{2}|\d{4})$/;    let USAformat = ''    let Univformat = ''    if (regTestUNiv.test(dateString)) {        Univformat = dateString    } else if (regTestUsa.test(dateString)){        USAformat = dateString    } else {        return dateString instanceof Date && !isNaN(dateString);    }    let year = '';    let month = '';    let day = '';    if (USAformat.length > 0){         [month,day,year] = USAformat.split(/[\/.-]/);    } else if(Univformat.length > 0){         [year,month,day] = Univformat.split(/[\/.-]/)    }    const parsedYear = parseInt(year, 10);    if (parsedYear < 100) {        // Adjust 2-digit year to 4-digit year        const currentYear = new Date().getFullYear();        const currentCentury = Math.floor(currentYear / 100) * 100;        const adjustedYear = currentCentury + parsedYear;        if (!regTestYear.test(adjustedYear)) {            return false;        }    }    const date = new Date(year, month - 1, day);    if (isNaN(date.getTime())) {        return false;    }    const parsedMonth = date.getMonth() + 1;    const parsedDay = date.getDate();    return (        parseInt(month, 10) === parsedMonth &&        parseInt(day, 10) === parsedDay    );}

you can test codes with the followings:

// Is the date valid Date objectconsole.log(isValidDate(new Date()), "T")// Does the date start with 0 for month and/or day console.log(isValidDate('2023.01.21'),"T") // trueconsole.log(isValidDate('2023.01.09'),"T") // trueconsole.log(isValidDate('2023.1.09'),"T") // true// Is the date divided by valid symbleconsole.log(isValidDate('2023/12/31'),"T")  // trueconsole.log(isValidDate('2023-12-31'),"T") // trueconsole.log(isValidDate('2023.12.31'),"T") // trueconsole.log(isValidDate('2023?12.31'),"F") // false// Is the date formatted in USA console.log(isValidDate('12/31/2050'),"T") // trueconsole.log(isValidDate('12/31/50'),"T") // true// Is the date out of rangeconsole.log(isValidDate('2023.2.29'),"F")  // falseconsole.log(isValidDate('2023.14.29'),"F")  // falseconsole.log(isValidDate('2023.01.32'),"F") // false//other testconsole.log(isValidDate('12/0/0'),"F")console.log(isValidDate('0/0/0'),"F")console.log(isValidDate('/120/0'),"F")console.log(isValidDate('boo'),"F")console.log(isValidDate('124'),"F")

Viewing latest article 47
Browse Latest Browse All 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>