How To Check If A Date Is After Another Date Using JavaScript?

Check if a Date is after another Date using JavaScript

There are many situations when you must check if a date is after another date. For example, if you want to build an app that tells you if one person is older than another. So, we will show you how to check if a date is after another date using JavaScript.

Check if a date is after another date using JavaScript

Here are 3 ways programmers use when they want to check whether a date is after another date.

Using comparison operators

The > operator is one of the useful comparison operators in JavaScript. It can be used to see if one value is greater than another. We will use the > operator to see if date one is greater than date two. If it is, it will return true; if not, it will return false. Like this:

const arthur = {
  name: 'Arthur',
  birthDay: '1997/8/15',
  company: 'LearnShareIT'
}

const willie = {
  name: 'Willie',
  birthDay: '2000/6/1',
  company: 'LearnShareIT'
}

const arthurBirthDay = new Date(arthur.birthDay)
const willieBirthDay = new Date(willie.birthDay)

// Use > operator to check if Arthur's birthday is after Willie's birthday
if (arthurBirthDay > willieBirthDay) {
  // Arthur's birthday is after Willie's birthday
  console.log("Arthur is younger than Willie")
} else {
  // Willie's birthday is after Arthur's birthday
  console.log("Willie is younger than Arthur")
}

Output:

Willie is younger than Arthur

Using Date.parse()

We’ve already covered the syntax of Date.parse() in the previous post. If you’d like to review it, you can read it here.

This method takes a date string as an argument. It returns the number of milliseconds since Unix time. We will use this method to get the number of milliseconds of two date variables. If the number of milliseconds of the first date is greater than the number of milliseconds of the second date, then the first date is after the second date and vice versa.

const arthur = {
  name: 'Arthur',
  birthDay: '1997/8/15',
  company: 'LearnShareIT'
}

const willie = {
  name: 'Willie',
  birthDay: '2000/6/1',
  company: 'LearnShareIT'
}

// Use Date.parse() to check if Arthur's birthday is after Willie's birthday
if (Date.parse(arthur.birthDay) > Date.parse(willie.birthDay)) {
  // Arthur's birthday is after Willie's birthday
  console.log("Arthur is younger than Willie")
} else {
  // Willie's birthday is after Arthur's birthday
  console.log("Willie is younger than Arthur")
}

Output:

Willie is younger than Arthur

Using getTime()

You can find out if one date is after another by using the Date constructor to create two dates and then using the getTime() method to compare them. The getTime() method returns the number of milliseconds since Unix time, so we can compare them in the same way we did above.

const arthur = {
  name: 'Arthur',
  birthDay: '1997/8/15',
  company: 'LearnShareIT'
}

const willie = {
  name: 'Willie',
  birthDay: '2000/6/1',
  company: 'LearnShareIT'
}

// Create two dates using Date constructor
const arthurBirthDay = new Date(arthur.birthDay)
const willieBirthDay = new Date(willie.birthDay)

// Use getTime() to check if Arthur's birthday is after Willie's birthday
if (arthurBirthDay.getTime() > willieBirthDay.getTime()) {
  // Arthur's birthday is after Willie's birthday
  console.log("Arthur is younger than Willie")
} else {
  // Willie's birthday is after Arthur's birthday
  console.log("Willie is younger than Arthur")
}

Output:

Willie is younger than Arthur

Summary

To check if a date is after another date using JavaScript quickly, you can use the > or < operators. We hope you can apply this knowledge to build an application for yourself.

Have a great day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *