104

My application has an HTML form with some inputs populated from the backend and other inputs being entered by the user (in a time input). An onChange function runs through each input when the user changes a value.

The inputs populated from the backend are converted to moment objects, the user-entered dates are mere strings. This means the onChange function encounters some moment objects, and some strings. I need to know which inputs are moment objects and which aren't.

What's the recommended method for testing if a variable is a moment object?

I've noticed moment objects have a _isAMomentObject property but I'm wondering if there's another way to test if a variable is a moment object.

Another option I've tried is calling moment on the variable regardless. This converts the string variables to moment objects and doesn't seem to effect existing moment objects.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184

4 Answers4

114

Moment has an isMoment method for just such a purpose. It is not particularly easy to find in the docs unless you know what to look for.

It first checks instanceof and then failing that (for instance in certain subclassing or cross-realm situations) it will test for the _isAMomentObject property.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • what if it's not strongly typed? moment.isMoment(input); – Don Thomas Boyle Feb 12 '19 at 19:01
  • 3
    @DonThomasBoyle this is JavaScript: there is no strong typing. 'Strong' is kind of an ambiguous term for typing, but by pretty much any possible definition (other than perhaps memory safety) JS types are weak. The only way that you can have that blow up is if `input` is `null` or `undefined`, literally any non-null JS object or even primitive value will just return a boolean. – Jared Smith Feb 12 '19 at 19:43
40

You can check if it is an instanceof moment:

moment() instanceof moment; // true
  • 7
    Only if the instance and moment itself are in the same window. – JAAulde Apr 06 '16 at 17:49
  • 2
    This is always true, because moment() would always create a moment object. Did you instead mean "obj instanceof moment;" ? – NoBrainer Nov 28 '18 at 17:43
  • The danger of using instanceof is that the instance might be created from one moment dependency but will test against the moment function from another dependency. I.e. this will break if the code that created the moment object used another creation function than the code that tests. An example of when this can happen is if the code that runs the test above is in a separate npm package that depends on another version of moment. – Henrik Hansson Oct 06 '20 at 07:25
3

moment() instanceof moment;

will always be true, because if you have

  • moment(undefined) instanceof moment
  • moment("hello") instanceof moment

you are always creating a moment object. So the only way is to check like this

  • moment(property).isValid()
Fabien Sartori
  • 235
  • 3
  • 10
-1

Pretty similar to the answer by @Fabien, I'm checking the object if the isValid function is available.

const checkMoment = (date) => {
    if(!date.isValid){ // check if it's not a moment object
        // do something if it's not moment object
        console.log('this is not a moment object');
    }
    else {
        // do something if it's a moment object
        console.log('this is a moment object');
    }
   
}
Anang Satria
  • 1,226
  • 1
  • 15
  • 19