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 object
console.log(isValidDate(new Date()), "T")
// Does the date start with 0 for month and/or day
console.log(isValidDate('2023.01.21'),"T") // true
console.log(isValidDate('2023.01.09'),"T") // true
console.log(isValidDate('2023.1.09'),"T") // true
// Is the date divided by valid symble
console.log(isValidDate('2023/12/31'),"T") // true
console.log(isValidDate('2023-12-31'),"T") // true
console.log(isValidDate('2023.12.31'),"T") // true
console.log(isValidDate('2023?12.31'),"F") // false
// Is the date formatted in USA
console.log(isValidDate('12/31/2050'),"T") // true
console.log(isValidDate('12/31/50'),"T") // true
// Is the date out of range
console.log(isValidDate('2023.2.29'),"F") // false
console.log(isValidDate('2023.14.29'),"F") // false
console.log(isValidDate('2023.01.32'),"F") // false
//other test
console.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")
return ( Object.prototype.toString.call(d) === "[object Date]" && !isNaN(d.getTime()) );
– Sorceif (!bool == false) return false;
– SorceDate.parse
is dependent on the implementation, see #3086437 – RefreshmentObject.prototype.toString.call(obj) === "[object Date]" && !isNaN(obj)
wins. Cool. However, the test for a Date instance should be separate from the test of whether it contains a valid time value as they are separate concerns. – Otherdirectedreturn d.toString() !== 'Invalid Date'
? I assume that value is defined in the spec and thus as reliable as e.g. values produced bytypeof
. – Liselisettadate == 'Invalid Date'
? – Wailoo