Date Validation
Validate date inputs
JavaScript Date Validation Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Check if valid date
function isValidDate(date) {
return date instanceof Date && !isNaN(date.getTime());
}
console.log('Valid:', isValidDate(new Date()));
console.log('Invalid:', isValidDate(new Date('invalid')));
// Method 2: Validate date string
function isValidDateString(dateString) {
const date = new Date(dateString);
return isValidDate(date) && dateString !== 'Invalid Date';
}
console.log('Valid string:', isValidDateString('2024-01-15'));
console.log('Invalid string:', isValidDateString('not-a-date'));
// Method 3: Validate date format
function isValidDateFormat(dateString, format = 'YYYY-MM-DD') {
const patterns = {
'YYYY-MM-DD': /^\d{4}-\d{2}-\d{2}$/,
'DD/MM/YYYY': /^\d{2}\/\d{2}\/\d{4}$/,
'MM/DD/YYYY': /^\d{2}\/\d{2}\/\d{4}$/
};
if (!patterns[format].test(dateString)) {
return false;
}
const date = new Date(dateString);
return isValidDate(date);
}
console.log('Valid format:', isValidDateFormat('2024-01-15', 'YYYY-MM-DD'));
// Method 4: Validate date range
function isDateInRange(date, minDate, maxDate) {
if (!isValidDate(date)) return false;
return date >= minDate && date <= maxDate;
}
const checkDate = new Date('2024-01-15');
const min = new Date('2024-01-01');
const max = new Date('2024-12-31');
console.log('In range:', isDateInRange(checkDate, min, max));
// Method 5: Validate future date
function isFutureDate(date) {
if (!isValidDate(date)) return false;
return date > new Date();
}
console.log('Future date:', isFutureDate(new Date('2025-01-01')));
// Method 6: Validate past date
function isPastDate(date) {
if (!isValidDate(date)) return false;
return date < new Date();
}
console.log('Past date:', isPastDate(new Date('2020-01-01')));
// Method 7: Validate date components
function validateDateComponents(year, month, day) {
if (year < 1900 || year > 2100) return false;
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
const date = new Date(year, month - 1, day);
return date.getFullYear() === year &&
date.getMonth() === month - 1 &&
date.getDate() === day;
}
console.log('Valid components:', validateDateComponents(2024, 1, 15));
console.log('Invalid components:', validateDateComponents(2024, 2, 30)); // Feb 30 doesn't existValid: true Invalid: false Valid string: true Invalid string: false Valid format: true In range: true Future date: true Past date: true Valid components: true Invalid components: false
Understanding Date Validation
Date validation ensures correct dates.
Validation Methods
Common Checks
Component Validation
Use Cases
Best Practices
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Date Validation
This JavaScript program is part of the "Date/Time Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.