20

Is there a way to validate if the String passed as time is valid using Moment JS?

The operations moment("2014-12-13 asdasd","YYYY-MM-DD LT").isValid() or moment("asdasd","LT").isValid() equate to true which ideally shouldn't happen.

My application uses multiple languages and it is really not possible for me to come up with a RegEx pattern to validate the string. For example, if I get the time string as "午前12時12分0秒", Moment JS should be able to validate this. I checked the source and found that time checking is not that strict in the library. I might have missed something. Please help.

2 Answers 2

40

As described in the documentation, as of moment 2.3.0, you can pass a third parameter true which turns on "strict parsing" mode.

moment("2014-12-13 asdasd","YYYY-MM-DD LT", true).isValid()   // false

moment("2014-12-13 12:34 PM","YYYY-MM-DD LT", true).isValid()   // true

The down side is that it must match the locale's format (i.e. the one supplied as the second argument) exactly. Since LT is equivalent to h:mm A in English, it will only accept 12-hour time without seconds. If you pass 24 hour time, or pass seconds, then it will fail.

moment("2014-12-13 12:34:00 PM","YYYY-MM-DD LT", true).isValid()   // false
moment("2014-12-13 15:00","YYYY-MM-DD LT", true).isValid()         // false

A better solution might be to pass multiple formats with strict parsing:

var formats = ["YYYY-MM-DD LT","YYYY-MM-DD h:mm:ss A","YYYY-MM-DD HH:mm:ss","YYYY-MM-DD HH:mm"];
moment("2014-12-13 12:34 PM", formats, true).isValid()     // true
moment("2014-12-13 15:00", formats, true).isValid()        // true
moment("2014-12-13 12:34:00 PM", formats, true).isValid()  // true
3
  • Thank you for pointing that out. I plan on using moment()._lang._longDateFormat.LT to validate the string, which would change along with locales and should work fine.
    – Chiranjib
    Sep 28, 2014 at 4:49
  • 1
    If you're using a current version (2.2.0+), try this instead: moment.localeData().longDateFormat('LT'). docs here Sep 28, 2014 at 6:31
  • 1
    How can I validate date with zone abbr like '2014-12-13 12:34 PM PST', the format 'YYYY-MM-DD h:mm:ss A z' didn't work here
    – RaT
    Sep 23, 2019 at 7:28
4

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
<script src="https://momentjs.com/downloads/moment.js"></script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.