/

How to Determine if a Date is Today in JavaScript

How to Determine if a Date is Today in JavaScript

Discover an easy way to determine if a Date object represents the current date and time.

To determine if a JavaScript Date object instance represents the current date, you can compare the day, month, and year of the date to the current day, month, and year. This can be achieved using the getDate(), getMonth(), and getFullYear() methods of the Date object. The current date can be obtained by using new Date().

Here is a simple function that checks if the given Date object represents today and returns true if it does:

1
2
3
4
5
6
7
8
const isToday = (someDate) => {
const today = new Date();
return (
someDate.getDate() === today.getDate() &&
someDate.getMonth() === today.getMonth() &&
someDate.getFullYear() === today.getFullYear()
);
};

You can use the function like this:

1
const today = isToday(myDate);

If the today variable is true, it means the myDate object represents today’s date.

For more information on working with Date objects in JavaScript, refer to the JavaScript Date guide.

tags: [“JavaScript”, “Date object”, “comparison”, “getCurrentDate”]