I have a Date
object and I want to check if it’s tomorrow.
const dateToCheck: Date //Sat Jun 01 2024 09:00:00 GMT-0700
I calculated tomorrow like this:
const tomorrow = moment().add(1, 'day')
The start and end of tomorrow are found like this:
tomorrow.startOf('day') //Sat Jun 01 2024 00:00:00 GMT-0700
tomorrow.endOf('day') //Sat Jun 01 2024 23:59:59 GMT-0700
When I try to determine if the date I’m checking is tomorrow, it’s false
:
var isTomorrow = moment(dateToCheck).isBetween(
tomorrow.startOf('day'),
tomorrow.endOf('day'),
)
console.log(isTomorrow) //false
As far as I can tell, isTomorrow
should be true
because Sat Jun 01 2024 09:00:00
is between Sat Jun 01 2024 00:00:00
and Sat Jun 01 2024 23:59:59
.
Any idea what I’m doing wrong?
2
Answers
For more accuracy I’d convert them to ECMAScript epoch and then treat them as integers…
Or something like that.
startOf
andendOf
modify the original Moment object. When you callisBetween(tomorrow.startOf('day'), tomorrow.endOf('day'))
theisBetween
function receives the same object for the start and end dates.You need to make copies of the
tomorrow
object when calling the function: