Languages
[Edit]
EN

TypeScript - compare two dates

0 points
Created by:
Paluskitten
356

In this article, we're going to have a look at how to compare two dates in TypeScript.

Quick solution:

 

Talking about date values comparison we need to look at the fact there are comparison operators that will be working and will not directly with date objects:

  • Working operators are: >, <, >= and <=,
  • Not working operators are: ==, ===, != and !===.

For not working operators it is necessary to use getTime() method and compare results, e.g. date1.getTime() === date2.getTime().

Common mistakes analysis:

Note: read this or this article to understans how == and === work for objects comparision. 

Look at the below examples to see how to use comparison operators:

1. Bigger-smaller operators example

Edit

In this section presented example uses fact, that it is possible to check that dates are not in relation bigger or smaller, so they are equal.

Output:

2. Equal-different operators example

Edit

The example presented in this section uses fact, if the Unix time of both dates is equal it means dates are equal.

Output:

Alternative titles

  1. TypeScript - check if dates are equal or not
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join