How to Compare Dates in TypeScript?

I was working on a TypeScript project where I had to compare two dates to check which one was earlier. First, I attempted to do it by comparing two numbers, but it didn’t work as expected. It has issues with time zones, different formats, and even errors with the time part of the date.

Later, I found out the methods and ways to compare dates in TypeScript, including the scenarios where we have to exclude the time or handle different time zones with dates.

In this blog, I’ll explain the different methods I’ve used to compare dates in TypeScript.

Understanding Date Objects in TypeScript

Before diving into comparisons, let’s quickly understand how TypeScript handles dates:

// Creating a date object in TypeScript
const today: Date = new Date();
const specificDate: Date = new Date('2025-06-25');

TypeScript uses JavaScript’s Date object under the hood, which means all the same methods are available, but with the added benefit of type safety.

Method 1: Using Comparison Operators

The simplest way to compare dates in TypeScript is to use standard comparison operators:

function compareDates(date1: Date, date2: Date): number {
  // Returns -1 if date1 is earlier, 0 if equal, 1 if date1 is later
  if (date1 < date2) return -1;
  if (date1 > date2) return 1;
  return 0;
}

// Example usage
const july4th2025 = new Date('2025-07-04');
const thanksgiving2025 = new Date('2025-11-27');

console.log(compareDates(july4th2025, thanksgiving2025)); // Outputs: -1

Output:

Compare Two dates in TypeScript

This method works because JavaScript Date objects are automatically converted to milliseconds when used with comparison operators.

Check out: Get Tomorrow’s Date in TypeScript

Method 2: Using getTime() for More Precision

For more explicit control, I prefer using the getTime() method, which returns the number of milliseconds since January 1, 1970:

function areDatesEqual(date1: Date, date2: Date): boolean {
  return date1.getTime() === date2.getTime();
}

function isDateBefore(date1: Date, date2: Date): boolean {
  return date1.getTime() < date2.getTime();
}

function isDateAfter(date1: Date, date2: Date): boolean {
  return date1.getTime() > date2.getTime();
}

// Example with US holidays
const laborDay2025 = new Date('2025-09-01');
const veteransDay2025 = new Date('2025-11-11');

console.log(isDateBefore(laborDay2025, veteransDay2025)); // Outputs: true

Output:

TypeScript Date Comparison

I find this approach more readable and explicit, making it my go-to method for date comparisons.

Check out: ISO Date Strings in TypeScript

Method 3: Comparing Only Date Parts (Ignoring Time)

When working on a scheduling application for a US client, I needed to compare only the date portion without considering the time. Here’s a pattern I developed:

function compareDatePartsOnly(date1: Date, date2: Date): number {
  // Create new dates with only the year, month, and day
  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();
}

// Example with time components
const morningMeeting = new Date('2025-12-15T09:00:00');
const afternoonMeeting = new Date('2025-12-15T14:30:00');

console.log(compareDatePartsOnly(morningMeeting, afternoonMeeting)); // Outputs: 0 (same day)

Output:

Compare dates in TypeScript without Time

This technique is invaluable when you’re only concerned with whether events occur on the same day.

Method 4: Using Date-fns Library

For more complex date operations, I recommend using a library like date-fns, which provides type definitions for TypeScript:

import { isEqual, isBefore, isAfter, compareAsc, compareDesc } from 'date-fns';

// Using date-fns functions
const independenceDay = new Date('2025-07-04');
const christmasDay = new Date('2025-12-25');

console.log(isEqual(independenceDay, christmasDay)); // false
console.log(isBefore(independenceDay, christmasDay)); // true
console.log(isAfter(independenceDay, christmasDay)); // false

// For sorting
const holidays = [
  new Date('2025-12-25'), // Christmas
  new Date('2025-07-04'), // Independence Day
  new Date('2025-11-27'), // Thanksgiving
];

// Sort in ascending order
const sortedAsc = [...holidays].sort(compareAsc);
console.log(sortedAsc.map(date => date.toDateString()));
// ["Tue Jul 04 2025", "Thu Nov 27 2025", "Mon Dec 25 2025"]

Output:

Handle Timezones in TypeScript date comparison

Date-fns is strongly typed and provides a comprehensive set of utilities, making it my preferred choice for production applications.

Check out: Add Days to a Date in TypeScript

Method 5: Handling Time Zones

Time zone differences can cause unexpected issues when comparing dates. Here’s how I handle them:

function compareDatesInUTC(date1: Date, date2: Date): number {
  const utcDate1 = Date.UTC(
    date1.getFullYear(),
    date1.getMonth(),
    date1.getDate()
  );

  const utcDate2 = Date.UTC(
    date2.getFullYear(),
    date2.getMonth(),
    date2.getDate()
  );

  return utcDate1 - utcDate2;
}

// Example with different time zones
const nyEvent = new Date('2025-12-31T23:00:00-05:00'); // New York New Year's Eve
const laEvent = new Date('2025-12-31T20:00:00-08:00'); // Los Angeles same time

console.log(compareDatesInUTC(nyEvent, laEvent)); // Outputs: 0 (same UTC day)

This approach normalizes dates to UTC, eliminating time zone discrepancies.

Method 6: Creating Custom Date Comparison Types

For more complex applications, I often create custom types to handle date comparisons:

enum DateComparisonResult {
  BEFORE = -1,
  EQUAL = 0,
  AFTER = 1
}

interface DateComparer {
  compare(date1: Date, date2: Date): DateComparisonResult;
  isBefore(date1: Date, date2: Date): boolean;
  isAfter(date1: Date, date2: Date): boolean;
  isEqual(date1: Date, date2: Date): boolean;
}

class StandardDateComparer implements DateComparer {
  compare(date1: Date, date2: Date): DateComparisonResult {
    const time1 = date1.getTime();
    const time2 = date2.getTime();

    if (time1 < time2) return DateComparisonResult.BEFORE;
    if (time1 > time2) return DateComparisonResult.AFTER;
    return DateComparisonResult.EQUAL;
  }

  isBefore(date1: Date, date2: Date): boolean {
    return this.compare(date1, date2) === DateComparisonResult.BEFORE;
  }

  isAfter(date1: Date, date2: Date): boolean {
    return this.compare(date1, date2) === DateComparisonResult.AFTER;
  }

  isEqual(date1: Date, date2: Date): boolean {
    return this.compare(date1, date2) === DateComparisonResult.EQUAL;
  }
}

// Usage
const dateComparer = new StandardDateComparer();
const blackFriday = new Date('2025-11-28');
const cyberMonday = new Date('2025-12-01');

console.log(dateComparer.isBefore(blackFriday, cyberMonday)); // true

Output:

Custom Date Comparison Types in TypeScript

This object-oriented approach makes the code more maintainable and provides a consistent API for date comparisons throughout your application.

Check out: Calculate Yesterday’s Date in TypeScript

Conclusion

In this TypeScript tutorial, we have learned about different ways to compare dates in TypeScript. For this, we used comparison operators, the getTime() method, and demonstrated how to compare only the date part without the time. We also learned how libraries like date-fns make date comparison easier and how to handle time zones properly. If you’re working on something more advanced, creating a custom date comparison utility can also help create reusable code for date comparison.

Remember that dates in JavaScript (and therefore TypeScript) can be tricky due to time zones and browser inconsistencies. Always test your date comparison logic thoroughly across different environments.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.