Date Validation in JavaScript

Validate date inputs

BeginnerDate/Time ProgramsExample 9 of 10
date-validation.js
1// Method 1: Check if valid date
2function isValidDate(date) {
3 return date instanceof Date && !isNaN(date.getTime());
4}
5
6console.log('Valid:', isValidDate(new Date()));
7console.log('Invalid:', isValidDate(new Date('invalid')));
8
9// Method 2: Validate date string
10function isValidDateString(dateString) {
11 const date = new Date(dateString);
12 return isValidDate(date) && dateString !== 'Invalid Date';
13}
14
15console.log('Valid string:', isValidDateString('2024-01-15'));
16console.log('Invalid string:', isValidDateString('not-a-date'));
17
18// Method 3: Validate date format
19function isValidDateFormat(dateString, format = 'YYYY-MM-DD') {
20 const patterns = {
21 'YYYY-MM-DD': /^\d{4}-\d{2}-\d{2}$/,
22 'DD/MM/YYYY': /^\d{2}\/\d{2}\/\d{4}$/,
23 'MM/DD/YYYY': /^\d{2}\/\d{2}\/\d{4}$/
24 };
25
26 if (!patterns[format].test(dateString)) {
27 return false;
28 }
29
30 const date = new Date(dateString);
31 return isValidDate(date);
32}
33
34console.log('Valid format:', isValidDateFormat('2024-01-15', 'YYYY-MM-DD'));
35
36// Method 4: Validate date range
37function isDateInRange(date, minDate, maxDate) {
38 if (!isValidDate(date)) return false;
39 return date >= minDate && date <= maxDate;
40}
41
42const checkDate = new Date('2024-01-15');
43const min = new Date('2024-01-01');
44const max = new Date('2024-12-31');
45console.log('In range:', isDateInRange(checkDate, min, max));
46
47// Method 5: Validate future date
48function isFutureDate(date) {
49 if (!isValidDate(date)) return false;
50 return date > new Date();
51}
52
53console.log('Future date:', isFutureDate(new Date('2025-01-01')));
54
55// Method 6: Validate past date
56function isPastDate(date) {
57 if (!isValidDate(date)) return false;
58 return date < new Date();
59}
60
61console.log('Past date:', isPastDate(new Date('2020-01-01')));
62
63// Method 7: Validate date components
64function validateDateComponents(year, month, day) {
65 if (year < 1900 || year > 2100) return false;
66 if (month < 1 || month > 12) return false;
67 if (day < 1 || day > 31) return false;
68
69 const date = new Date(year, month - 1, day);
70 return date.getFullYear() === year &&
71 date.getMonth() === month - 1 &&
72 date.getDate() === day;
73}
74
75console.log('Valid components:', validateDateComponents(2024, 1, 15));
76console.log('Invalid components:', validateDateComponents(2024, 2, 30)); // Feb 30 doesn't exist

Output

Valid: 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

What's going on

Date validation ensures correct dates.

Validation Methods

Check Date object validity
Validate date strings
Check format patterns
Validate components

Common Checks

Valid date object
Correct format
In range
Future/past

Component Validation

Year range
Month (1-12)
Day (1-31)
Handle leap years

Use Cases

Form validation
Input sanitization
Data integrity
Error prevention

Best Practices

Validate early
Provide clear errors
Handle edge cases
Test thoroughly