Open In App

How to Compare Two Date Strings in TypeScript ?

In TypeScript, we can compare two date strings by converting them to comparable formats using Date objects or by parsing them into the timestamps. We will discuss different approaches with the practical implementation of each one of them.

Using Date Objects

In this approach, we are using Date objects to parse the input date strings. We then compare their time values using getTime(), and based on the comparison, we print whether the first date is equal to, earlier than, or later than the second date.

Syntax:

const res: Date = new Date();

Example: The below example uses Date Objects to compare two date strings in TypeScript.

const dStr1: string = "2022-02-27";
const dStr2: string = "2024-02-27";
const d1: Date = new Date(dStr1);
const d2: Date = new Date(dStr2);
if (d1.getTime() === d2.getTime()) {
  console.log(`${dStr1} is equal to ${dStr2}`);
} else if (d1.getTime() < d2.getTime()) {
  console.log(`${dStr1} is earlier than ${dStr2}`);
} else {
  console.log(`${dStr2} is earlier than ${dStr1}`);
}

Output:

2022-02-27 is earlier than 2024-02-27

Using Date.parse

In this approach, we are using the Date.parse method to convert the input date strings into timestamps. We then compare these timestamps and print whether the first date is equal to, earlier than, or later than the second date.

Syntax:

const res: number = Date.parse(dateString);

Example: The below example uses Date.parse to compare two date strings in TypeScript.

const dStr1: string = '2022-02-27';
const dStr2: string = '2024-02-27';
const t1: number = Date.parse(dStr1);
const t2: number = Date.parse(dStr2);
if (t1 === t2) {
    console.log(`${dStr1} is equal to ${dStr2}`);
} else if (t1 < t2) {
    console.log(`${dStr1} is earlier than ${dStr2}`);
} else {
    console.log(`${dStr2} is earlier than ${dStr1}`);
}

Output:

2022-02-27 is earlier than 2024-02-27

Using Intl.DateTimeFormat

In this approach, we are using Intl.DateTimeFormat to create a formatter with specific date formatting options for the 'en-US' locale. We then format the input date strings using this formatter. The formatted dates are compared, and based on the comparison, we print whether the first date is equal to, earlier than, or later than the second date.

Syntax:

const formatter: Intl.DateTimeFormat = 
    new Intl.DateTimeFormat(locales, options); 

Example: The below example uses Intl.DateTimeFormat to compare two date strings in TypeScript.

const dStr1: string = '2024-02-27';
const dStr2: string = '2024-02-27';
const temp = new Intl.DateTimeFormat('en-US',
    {
        year: 'numeric', month: '2-digit',
        day: '2-digit'
    });
const d1 = temp.format(new Date(dStr1));
const d2 = temp.format(new Date(dStr2));
if (d1 === d2) {
    console.log(`${dStr1} is equal to ${dStr2}`);
} else if (d1 < d2) {
    console.log(`${dStr1} is earlier than ${dStr2}`);
} else {
    console.log(`${dStr2} is earlier than ${dStr1}`);
}

Output:

2024-02-27 is equal to 2024-02-27

Using a Custom Date Comparison Function

In this approach, a custom function is implemented to compare two date strings based on specific requirements or formats. This approach provides flexibility to tailor the comparison logic according to the application's needs.

Syntax:

function compareDates(dateString1: string, dateString2: string): number {
    // Custom comparison logic
}

Example: In this example Function compareDates compares two date strings, returning 0 if equal, -1 if first is earlier, and 1 if later. It then logs the comparison result.

function compareDates(dateString1: string, dateString2: string): number {
    const date1: Date = new Date(dateString1);
    const date2: Date = new Date(dateString2);

    if (date1.getTime() === date2.getTime()) {
        return 0; // Dates are equal
    } else if (date1.getTime() < date2.getTime()) {
        return -1; // First date is earlier than the second date
    } else {
        return 1; // First date is later than the second date
    }
}

const dStr1: string = '2024-02-27';
const dStr2: string = '2024-02-27';
const comparisonResult: number = compareDates(dStr1, dStr2);

if (comparisonResult === 0) {
    console.log(`${dStr1} is equal to ${dStr2}`);
} else if (comparisonResult < 0) {
    console.log(`${dStr1} is earlier than ${dStr2}`);
} else {
    console.log(`${dStr2} is earlier than ${dStr1}`);
}

Output:

2024-02-27 is equal to 2024-02-27
Article Tags :