function sanitizeDateInput(input) {
// Remove leading/trailing whitespace
input = input.trim();
// Check if the input is a valid date string
if (isValidDate(input)) {
return input; // Return as is if it's already valid
}
// Attempt to parse the date using a common format (YYYY-MM-DD)
const parsedDate = new Date(input);
if (!isNaN(parsedDate.getTime())) {
return input; //Return if parsing was successful
}
// If parsing fails, return null or an error message (you can customize this)
return null; // Or return "Invalid date format";
}
function isValidDate(dateString) {
//Basic validation to check if the string can be parsed as a date
try {
new Date(dateString);
return true;
} catch (e) {
return false;
}
}
export default sanitizeDateInput;
Add your comment