date comparison
programming
datetime
coding tips
software development

How to compare two Dates without the time portion?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Comparing two date-time values while ignoring the time portion is a common requirement, but the correct technique depends on the platform and the meaning of the data. The real rule is simple: compare values after converting them into a date-only representation in the correct time zone.

The important part is that "ignore the time" does not mean "just chop off characters" or "subtract timestamps and hope." It means comparing the calendar date that each value represents.

Compare Date-Only Values, Not Raw Date-Times

If a language or library offers a date-only type, that is usually the safest choice. For example, in Python:

python
1from datetime import datetime
2
3first = datetime(2024, 10, 12, 14, 30)
4second = datetime(2024, 10, 12, 10, 45)
5
6same_day = first.date() == second.date()
7print(same_day)

The comparison becomes unambiguous because both values are reduced to their year-month-day component first.

Java: Prefer LocalDate

In Java, the clean answer is to convert LocalDateTime or other date-time values to LocalDate before comparing:

java
1import java.time.LocalDate;
2import java.time.LocalDateTime;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalDateTime first = LocalDateTime.of(2024, 10, 12, 14, 30);
7        LocalDateTime second = LocalDateTime.of(2024, 10, 12, 10, 45);
8
9        LocalDate d1 = first.toLocalDate();
10        LocalDate d2 = second.toLocalDate();
11
12        System.out.println(d1.equals(d2));
13    }
14}

This avoids time-of-day issues entirely because LocalDate contains only the calendar date.

JavaScript: Normalize to a Date Boundary Carefully

In JavaScript, be explicit about which time zone you are using. For local-calendar comparison:

javascript
1function sameLocalDay(a, b) {
2  return (
3    a.getFullYear() === b.getFullYear() &&
4    a.getMonth() === b.getMonth() &&
5    a.getDate() === b.getDate()
6  );
7}
8
9const first = new Date("2024-10-12T14:30:00");
10const second = new Date("2024-10-12T10:45:00");
11
12console.log(sameLocalDay(first, second));

If the comparison should be based on UTC rather than local time, use the UTC getters instead. Time zone choice is part of the problem, not a separate detail.

SQL: Cast or Extract the Date Part

In databases, the pattern is the same: compare date-only values, not full timestamps.

MySQL:

sql
SELECT DATE(created_at) = DATE(updated_at) AS same_day
FROM events;

PostgreSQL:

sql
SELECT created_at::date = updated_at::date AS same_day
FROM events;

Be careful with large queries, though. Wrapping columns in functions can affect index usage, so for high-performance filters you may prefer explicit date ranges instead of casting in the predicate.

Time Zone Choice Comes First

This is where many bugs come from. Suppose two timestamps represent the same instant but display as different calendar dates in different zones. If the business rule is "same local day for the user," compare after converting both values to that local zone. If the rule is "same UTC day," normalize to UTC first.

Ignoring the time portion without deciding the time zone is not actually a complete solution.

Common Pitfalls

The biggest mistake is comparing string prefixes or manually truncating formatted timestamps. That works only as long as formatting never changes and time zones never matter.

Another common issue is comparing two instants in different time zones without first deciding which calendar the comparison should use. The same moment can belong to different dates in different zones.

Developers also hurt performance in SQL by wrapping indexed columns in date-extraction functions inside high-volume filters. For equality checks in application code that is fine, but for database search predicates it can be worth using range comparisons instead.

Finally, avoid subtracting timestamps and dividing by hours to infer "same day." That is the wrong abstraction for a calendar-date question.

Summary

  • To compare dates without time, first convert both values into a date-only representation.
  • Use types such as date() in Python and LocalDate in Java when available.
  • In JavaScript and SQL, make the time zone and extraction strategy explicit.
  • Decide whether the comparison is based on local time or UTC before comparing calendar dates.
  • Do not rely on string slicing or raw timestamp arithmetic for a date-only comparison problem.

Course illustration
Course illustration

All Rights Reserved.