Compare Dates
Compare two dates
BeginnerTopic: Date/Time Programs
JavaScript Compare Dates Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Compare timestamps
function compareDates(date1, date2) {
if (date1.getTime() === date2.getTime()) {
return 0; // Equal
} else if (date1.getTime() > date2.getTime()) {
return 1; // date1 is later
} else {
return -1; // date1 is earlier
}
}
const date1 = new Date('2024-01-15');
const date2 = new Date('2024-01-20');
console.log('Compare:', compareDates(date1, date2));
// Method 2: Simple comparisons
console.log('date1 > date2:', date1 > date2);
console.log('date1 < date2:', date1 < date2);
console.log('date1 === date2:', date1.getTime() === date2.getTime());
// Method 3: Check if date is in range
function isDateInRange(date, startDate, endDate) {
return date >= startDate && date <= endDate;
}
const checkDate = new Date('2024-01-18');
const start = new Date('2024-01-15');
const end = new Date('2024-01-20');
console.log('In range:', isDateInRange(checkDate, start, end));
// Method 4: Check if date is today
function isToday(date) {
const today = new Date();
return date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear();
}
console.log('Is today:', isToday(new Date()));
// Method 5: Check if date is past/future
function isPast(date) {
return date < new Date();
}
function isFuture(date) {
return date > new Date();
}
console.log('Is past:', isPast(new Date('2020-01-01')));
console.log('Is future:', isFuture(new Date('2025-01-01')));
// Method 6: Compare dates (ignore time)
function compareDatesOnly(date1, date2) {
const d1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
const d2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
return d1.getTime() - d2.getTime();
}
const dateWithTime = new Date('2024-01-15T15:30:00');
const dateWithoutTime = new Date('2024-01-15T00:00:00');
console.log('Same date (ignore time):', compareDatesOnly(dateWithTime, dateWithoutTime) === 0);Output
Compare: -1 date1 > date2: false date1 < date2: true date1 === date2: false In range: true Is today: true Is past: true Is future: true Same date (ignore time): true
Understanding Compare Dates
Date comparison checks relationships.
Comparison Methods
•getTime(): Compare timestamps
•Direct comparison: >, <, ===
•Date methods: getDate, getMonth, etc.
Common Checks
•Equal: getTime() === getTime()
•Before: date1 < date2
•After: date1 > date2
•In range: start <= date <= end
Date-Only Comparison
•Ignore time component
•Compare year/month/day only
•Useful for date ranges
Best Practices
•Use getTime() for equality
•Handle timezone issues
•Compare dates only when needed
•Use helper functions
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.