How to determine whether the date is today in JavaScript

Explore how to find out whether the Date object represents today's date and time

How to determine whether the JavaScript Date object instance represents the "today" date/time?

Given a Date instance, we can usegetDate(),getMonth()withgetFullYear()Method, the method returns the day, month and year of the date, and compares them with today, you can use the retrievalnew Date().

This is a small function that fully meets this requirement. If the argument is today, it returns true.

const isToday = (someDate) => {
  const today = new Date()
  return someDate.getDate() == today.getDate() &&
    someDate.getMonth() == today.getMonth() &&
    someDate.getFullYear() == today.getFullYear()

You can use it like this:

const today = isToday(myDate)

Take a lookJavaScript date guideLearn more about how to deal with Date objects (if needed).

Download mine for freeNode.js manual

More node tutorials: