Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary deprecation warning #2535

Closed
SVasilev opened this issue Aug 3, 2015 · 9 comments
Closed

Unnecessary deprecation warning #2535

SVasilev opened this issue Aug 3, 2015 · 9 comments

Comments

@SVasilev
Copy link

SVasilev commented Aug 3, 2015

Hello,
I am using moment js for my application and I faced a problem. So I have something like this:

if (!moment(inputValue).isValid()) {
  // some stuff going on here...
}

As you see I am trying to use the isValid() method only for verification. Sometimes my input is not a valid date for moment js so it produces the "Deprecation warning: moment construction falls back to js Date." message.
I understand why you have put deprecation warning for the moment constructor but in my case I only want to use moment for verification. Shouldn't there be a way to use the isValid() functionality without getting the deprecation message. I need this because of some logging I have to handle.
Best regards,
Stefan.

@mattjohnsonpint
Copy link
Contributor

You still need to supply one or more format strings. Moment can reliably test if a string can be parsed to a valid date in a particular format, but it cannot reliably test if any string is a date.

There are many reasons for this, such as locale format differences.

@vachkov
Copy link

vachkov commented Aug 4, 2015

The point is that moment supports some default formats. So if you want to test if some string can be parsed by moment out of the box and the string is not parseable - then you get something printed in the console and a moment object with method .isValid(), which is strange and you cannot control it. It would be convenient to have a way to at least suppress dumping in the console - everything stays the same.

best regards,
Georgi

@mattjohnsonpint
Copy link
Contributor

I think the proposal in #2469 would cover that. Then you could do moment(string, true).isValid(), and it would work for all the formats that moment supports by default.

@SVasilev
Copy link
Author

SVasilev commented Aug 4, 2015

Thanks,
I am glad that you looked further. I am looking forward for your fix :)
Stefan

@vachkov
Copy link

vachkov commented Aug 5, 2015

Thanks for the proposal Matt, looks fine.

best regards,
Georgi

@odigity
Copy link

odigity commented Mar 23, 2017

I found this issue while googling for a solution to the same problem as @SVasilev. I just want to see if a string is parseable by Moment.js (not knowing the format in advance) without generating deprecation warnings. Would be wonderful if there were a class function instead of instance function, like moment.isValid() that just returns a boolean, rather than having to use the constructor first and get a deprecation warning.

@mattjohnsonpint
Copy link
Contributor

@odigity - Supply some format or formats that you might be expecting and you won't get a deprecation warning. Checking for any string that might be a date is an impossibility though, because with strings like 01/02/03 one cannot tell which order the fields are in.

Also, please don't comment on old closed issues to ask for new features. Just open a new issue. Thanks.

@sam-araiza
Copy link

sam-araiza commented Jun 12, 2019

Why is this still an issue in 2019? I should not get a deprecation warning when the provided string is invalid if I'm calling .isValid(). I'm purposely throwing invalid strings when unit testing a function and I want it to throw without this warning cluttering my logs. Thanks.

A limited workaround that I'm using for now is if (!Boolean(Date.parse('invalid date'))) throw ..
Would like to use if (!moment('invalid date').isValid) .. for the cases of Feb 30th and such.

Note—this is a not a new feature request, but the original request from @SVasilev

@hefox
Copy link

hefox commented Oct 17, 2019

This issue is closed, not sure how to re-open

The moment docs actually has an example https://momentjs.com/docs/ that'd produce this warning

moment("not a real date").isValid();

I think that needs to be updated at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants