Recently, while working on a TypeScript feature for an internal tool, I needed to compare two dates, but only the date part, not the time. At first, I thought it would be straightforward, but I realised there are multiple ways to do it, and the results can vary depending on the method used.
I tried different approaches available in TypeScript to compare dates without considering the time. In this tutorial, I will explain how to compare dates without considering the time component in TypeScript.
Converting Date Strings to Date Objects
Before we start comparing dates, we must ensure that we are working with Date objects. If you have date strings, parse them into Date objects. TypeScript provides a convenient way to do this using the Date constructor. Here’s an example:
const dateString1 = "2025-04-15";
const dateString2 = "2025-04-15T09:30:00";
const date1 = new Date(dateString1);
const date2 = new Date(dateString2);In the above code, we have two date strings: dateString1 represents only the date part, while dateString2 includes both the date and time. By passing these strings to the Date constructor, we create corresponding Date objects date1 and date2.
Check out: Subtract Dates in TypeScript
Comparing Dates Using the toDateString() Method
One straightforward approach to compare dates without considering the time is to use the toDateString() method. This method returns a string representation of the date portion of a Date object. Here’s an example:
const date1 = new Date("2025-06-15");
const date2 = new Date("2025-06-15T09:30:00");
if (date1.toDateString() === date2.toDateString()) {
console.log("The dates are equal.");
} else {
console.log("The dates are not equal.");
}In this example, we compare the date strings returned by toDateString(). Since both date1 and date2 have the same date part (June 15, 2025), the condition date1.toDateString() === date2.toDateString() evaluates to true, and the output will be “The dates are equal.”
Output:

Comparing Dates by Setting the Time to Midnight
Another approach to compare dates without time is to set the time component of both dates to midnight (00:00:00). By doing this, we essentially remove the time factor from the comparison. Here’s an example:
const date1 = new Date("2025-06-15T08:00:00");
const date2 = new Date("2025-06-15T12:30:00");
date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);
if (date1.getTime() === date2.getTime()) {
console.log("The dates are equal.");
} else {
console.log("The dates are not equal.");
}Output:

In this example, we use the setHours() method to set the hours, minutes, seconds, and milliseconds of both date1 and date2 to zero. This effectively sets the time to midnight. Then, we compare the timestamps of the modified dates using the getTime() method. If the timestamps are equal, it means the dates are equal.
Check out: How to Get the Current Date in Milliseconds Using TypeScript
Comparing Dates Using a Custom Function
For more flexibility and reusability, you can create a custom function to compare dates without time. Here’s an example:
function compareDatesWithoutTime(date1: Date, date2: Date): boolean {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}
const date1 = new Date("2025-06-15T08:00:00");
const date2 = new Date("2025-06-15T12:30:00");
if (compareDatesWithoutTime(date1, date2)) {
console.log("The dates are equal.");
} else {
console.log("The dates are not equal.");
}Output:

In this approach, we define a custom function compareDatesWithoutTime that takes two Date objects as parameters. Inside the function, we compare the year, month, and day components of the dates using the getFullYear(), getMonth(), and getDate() methods, respectively.
If all three components are equal, the function returns true, indicating that the dates are equal without considering the time.
Check out: How to Get Tomorrow’s Date in TypeScript
Real-World Example: Checking Birthdays
Let’s consider a real-world scenario where you need to check if today is someone’s birthday. We’ll use the custom function approach to compare dates without time.
function compareDatesWithoutTime(date1: Date, date2: Date): boolean {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}
const today = new Date();
const johnBirthday = new Date("1990-06-15");
const sarahBirthday = new Date("1985-09-20");
if (compareDatesWithoutTime(today, johnBirthday)) {
console.log("Happy birthday, John!");
} else if (compareDatesWithoutTime(today, sarahBirthday)) {
console.log("Happy birthday, Sarah!");
} else {
console.log("No birthdays today.");
}Output:

In this example, we store two birthdays as Date objects: johnBirthday and sarahBirthday. We compare each birthday with today’s date using the compareDatesWithoutTime function. If any of the comparisons return true, it means today is that person’s birthday, and we print a corresponding message. If none of the comparisons match, we print “No birthdays today.”
Check out: How to Format Dates in TypeScript
Conclusion
Comparing dates without time in TypeScript can be accomplished using various methods. You can convert date strings to Date objects, use the toDateString() method to compare date strings, set the time to midnight for comparison, or create a custom function for more flexibility. Choose the approach that best fits your specific use case and requirements.
Remember to handle edge cases and validate input formats when working with dates to ensure accurate comparisons. With these techniques, you can effectively compare dates without considering the time component in your TypeScript applications.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.