Matt Arnold
Matt Arnold

Reputation: 686

moment's isSameOrBefore and isSameOrAfter return "is not a function" when called from a moment and passed a moment

Why do momentjs isSameOrBefore and isSameOrAfter functions does not work for data which isBefore and isAfter work for?

Here are some very simple isolated examples where isSameOrBefore and isSameOrAfter does not work:

moment("2018-10-12 10:44:01").isSameOrBefore(moment("2018-10-12 10:44:00"))
TypeError: moment(...).isSameOrBefore is not a function [Learn More]

moment("2018-10-12 10:44:01").isSameOrAfter(moment("2018-10-12 10:44:02"))
TypeError: moment(...).isSameOrAfter is not a function [Learn More]

But yet with the same kind of data, isBefore and isAfter work fine:

moment("2018-10-12 10:44:01").isBefore(moment("2018-10-12 10:44:00"))
false
moment("2018-10-12 10:44:01").isAfter(moment("2018-10-12 10:44:02"))
false
moment("2018-10-12 10:44:00").isBefore(moment("2018-10-12 10:44:01"))
true
moment("2018-10-12 10:44:02").isAfter(moment("2018-10-12 10:44:01"))
true

Upvotes: 7

Views: 20051

Answers (2)

Matt Arnold
Matt Arnold

Reputation: 686

The solution to this for me was to update to version 2.23.0 of Moment.js. I was mislead by my moment.d.ts file which, for unknown reasons, was for version 2.11.1 of Moment.js instead as opposed to the version I had installed (version 2.9.0).

After the update, be sure to check in the browser's debugger's file explorer for the moment.js file and verify that it matches up with the one in your project - if it doesn't, at least in Firefox/Waterfox, try restarting the browser then pressing Ctrl + F5 on the page of your project you're having the issue with.

This has been a lesson for me to not put so much faith in the type-safety of TypeScript!

Upvotes: 5

veben
veben

Reputation: 22322

The isSameOrBefore and isSameOrAfter methods were introduced since version 2.11.0 : https://momentjs.com/docs/#/query/is-same-or-before/

You can upgrade the version and try this:

moment('2018-10-12 10:44:01').isSameOrBefore('2018-10-12 10:44:00')

Upvotes: 4

Related Questions