Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Sat May 13 2023

How to check if two dates are in the same day in plain JavaScript

A while back, I came across a situation where I wanted to check if two dates are on the same day, tomorrow or further away. And I thought I would share how I implemented it and hopefully help someone else out.

Working with dates can sometimes be a little bit cumbersome, hence, libraries like for example date-fns is super popular since they are providing a lot of great utilities for working with dates. However, sometimes it makes more sense to use our own knowledge to solve it without any external libs - in my case, I was working on our weather widget and I didn't want it to have any dependencies except React.

Solution

I will implement a function in this example, that will take in a date as an argument, and then check if it is today, tomorrow, and if it is another day, it will format it to the weekday and return it.

We will create two seperate functions to do the today and tomorrow checks to keep our main function a little bit cleaner

const isToday = (date) => { const today = new Date().setHours(0, 0, 0, 0); const checkToday = date.setHours(0, 0, 0, 0); return checkToday === today; }; const isTomorrow = (date) => { const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); return ( tomorrow.getFullYear() === date.getFullYear() && tomorrow.getMonth() === date.getMonth() && tomorrow.getDate() === date.getDate() ); }; const formatDay = (date) => { if (isToday(date)) { return "Today"; } else if (isTomorrow(date)) { return "Tomorrow"; } return date.toLocaleDateString("en-us", { weekday: "long" }); }; const today = new Date(); const tomorrow = new Date(); tomorrow.setDate(new Date().getDate() + 1); const monday = new Date("2023-05-15"); const wednesday = new Date("2023-05-17"); console.log(formatDay(today)); // Today console.log(formatDay(tomorrow)); // Tomorrow console.log(formatDay(monday)); // Monday console.log(formatDay(wednesday)); // Wednesday

I will explain the important parts below

  • isToday() will basically reset the hours and check if the date is the same as the newly created Date object. If it is the same milliseconds returned from the setHours function, it means it is the same day.

  • isTomorrow() will basically do a check if it is on the same day. But we will only use it for our tomorrow scenario, hence we will always create a new Date object that will set the date to the next day to do our check. I will share example below how we can create a util function for checking if two dates are on the same day

  • If neither is returning true, we will format the day to the full weekday string.

  • And then we are creating our test data to see if it works.

Checking for the same day

If you want to create a util function for checking if two dates are on the same day, you can do as follows. Just change our isTomorrow() function a little bit

const isSameDay = (date1, date2) => { return ( date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate() ); }; console.log(isSameDay(today, tomorrow)); // false console.log(isSameDay(today, new Date("2023-05-13"))); // true (2023-05-13 is todays date)

And now we can use that in our project as a handy utility function.

Outro

In this guide, we created some nice and handy functions for checking dates in JavaScript. We can now see if a date is today, tomorrow or if two dates are on the same day. As stated above, working with dates can sometimes be a little bit tricky, but with some training and broken keyboards - it gets easier :)

I hope you enjoyed this guide, and have a great day!

All the best,

signatureSat May 13 2023
See all our articles