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

isValid not works correctly #5270

Closed
odykyi opened this issue Oct 28, 2019 · 4 comments
Closed

isValid not works correctly #5270

odykyi opened this issue Oct 28, 2019 · 4 comments

Comments

@odykyi
Copy link

odykyi commented Oct 28, 2019

Describe the bug
isValid not works correctly

To Reproduce
Steps to reproduce the behavior:
moment('Chicago Illinois 46702').isValid(); // true

Expected behavior
should be false

@ashsearle
Copy link
Contributor

It's valid as it's been turned into a valid date:

new Date('Chicago Illinois 46702'); // Wed Jan 01 46702 00:00:00 GMT+0000 (Greenwich Mean Time)

However... you should also see a warning from moment:

moment('Chicago Illinois 46702').isValid(); 

In your JavaScript console:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

@anticoagulant
Copy link

anticoagulant commented Oct 29, 2019

How about this though

moment('22-10-2020', 'dd-MM-yyyy', true).isValid();

returns false.

EDIT
I see: moment('22-10-2020', 'DD-MM-YYYY', true).isValid(); works

@odykyi
Copy link
Author

odykyi commented Oct 30, 2019

const allPossibleFormats = [
  'D MMMM YYYY',
  'D MMMM YYYY HH:mm',
  'DD-MM-YY',
  'DD-MM-YYYY',
  'DD.MM.YYYY',
  'DD.MM.YYYY HH:mm',
  'DD/MM/YY',
  'DD/MM/YYYY',
  'DD/MM/YYYY HH:mm:ss',
  'HH:mm:ss',
  'M/D/YYYY',
  'D/M/YYYY',
  'MM-DD-YY',
  'MM-DD-YYYY',
  'MM-DD-YYYY HH:mm:ss',
  'MM/DD/YY',
  'MM/DD/YYYY',
  'MM/DD/YYYY HH:mm:ss',
  'MMM D YYYY',
  'MMM D YYYY LT',
  'MMMM Do YYYY',
  'MMMM Do YYYY LT',
  'YYYY-DD-MM HH:mm:ss',
  'YYYY-MM',
  'YYYY-MM-DD',
  'YYYY-MM-DD HH:mm',
  'YYYY-MM-DD HH:mm:ss',
  'YYYY-MM-DD LT',
  'YYYY-MM-DD h:mm:ss A',
  'YYYY-MM-DDTHH:mm:ssZ',
  'ddd, MMM D YYYY LT',
  'dddd D MMMM YYYY HH:mm',
  'dddd, MMMM Do YYYY LT'
];

moment('Chicago Illinois 46702', allPossibleFormats, true).isValid(); // false
moment('18/01/1944', allPossibleFormats, true).isValid(); // true
moment('22-10-2020', allPossibleFormats, true).isValid(); // true
moment('1944-01-18 12:00:00', allPossibleFormats, true).isValid(); // true
moment('2001-01-01 00:00:00', allPossibleFormats, true).isValid(); // true
moment('2001-01', allPossibleFormats, true).isValid(); // true

Also we can extend allPossibleFormats array and add another date/datetime formats

@marwahaha
Copy link
Member

Please re-open if you still have issues.

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

4 participants