how to tell if two dates are in the same day? [duplicate]

I am using the moment npm module. I am comparing two dates and want to see if there in the same day.

Is there a clean way of doing that using the moment package or using straight javascript or typescript?



The Date prototype has APIs that allow you to check the year, month, and day-of-month, which seems simple and effective.

You'll want to decide whether your application needs the dates to be the same from the point of view of the locale where your code runs, 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();

There are corresponding UTC getters getUTCFullYear(), getUTCMonth(), and getUTCDate().

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

That will ensure the dates are in the same day.

Read more about Javascript Date to string


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.