How to find out if two dates are on the same day?

I am using the npm moment module. I am comparing two dates and want to see if there is one there that day.

Is there a clean way to do this using a moment package or using direct javascript or typescript?

+5
source share
2 answers

The Date prototype has an API that allows you to check the year, month, and day of the month, which seems simple and efficient.

You will need to decide whether your application should match the dates in terms of the locale where your code is running, or if the comparison should be based on UTC values.

function sameDay(d1, d2) { return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate(); } 

The corresponding UTC coefficients are getUTCFullYear() , getUTCMonth() and getUTCDate() .

+11
source
  var isSameDay = (dateToCheck.getDate() === actualDate.getDate() && dateToCheck.getMonth() === actualDate.getMonth() && dateToCheck.getFullYear() === actualDate.getFullYear()) 

This ensures that the dates are on the same day.

More on Javascript Date in a String

+2
source

All Articles