26

I'm trying to validate a ISO 8601 date in javascript using moment.js

console.log(moment("2011-10-10T14:48:00", "YYYY-MM-DD", true).isValid())

It returns false. Where am I going wrong ? Is the date type format incorrect ?

version: Moment 2.5.1

Praveen
  • 55,303
  • 33
  • 133
  • 164
user1184100
  • 6,742
  • 29
  • 80
  • 121
  • **No** it is returning `true`. http://jsfiddle.net/praveen_jegan/r42jg/49/ – Praveen Mar 04 '14 at 06:31
  • Hmm I'm using Moment 2.5.1 & I get false, Check the plnk - http://plnkr.co/edit/umqpqkqOgfRNeh5Gcafw?p=preview – user1184100 Mar 04 '14 at 06:36
  • 1
    thank you for the answers and for reverting the down vote and apologies for not going through the docs :) – user1184100 Mar 04 '14 at 06:45
  • FYI Basic ISO strings (e.g. 20111010) are not yet supported by moment, but are planned to be supported - https://github.com/moment/moment/issues/2025 – sync Mar 03 '15 at 23:21

4 Answers4

69

To avoid using string pattern as a second argument, you can just call:

moment("2011-10-10T14:48:00", moment.ISO_8601).isValid() // true
moment("2016-10-13T08:35:47.510Z", moment.ISO_8601).isValid() // true
wawka
  • 4,828
  • 3
  • 28
  • 22
  • 12
    you can try `moment("1466113", moment.ISO_8601, true).isValid()` to see that avoiding string pattern does not work everywhere – WhiteKnight Oct 10 '17 at 14:22
  • @WhiteKnight According to [Wikipedia](https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates), YYYYMMDD is a valid ISO8601 date. If you only want to form with the hyphens, then you are actually only targeting a subset of ISO8601. – Daniel Cheung Feb 25 '21 at 06:21
17

Not sure why Praveen's example works in jsfiddle, but the reason your sample doesn't work is because the format isn't YYYY-MM-DD. It includes the time as well, so it's considered invalid. If you try it without the time in the date, it returns true.

Try this instead:
moment("2011-10-10T14:48:00", "YYYY-MM-DDTHH:mm:ss", true).isValid()

antimatter
  • 3,240
  • 2
  • 23
  • 34
5

Okay, I found it.

As per the documentation,

As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly

because you use strict operation, it returns false. To overcome that use below code:

alert(moment("2011-10-10T14:48:00", "YYYY-MM-DDTHH:mm:ss", true).isValid())
//This will return true

demo1

If you remove the strict parsing,

alert(moment("2011-10-10T14:48:00", "YYYY-MM-DD").isValid())
//This will return true

demo2

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • this returns true, i expect it to be false alert(moment("6205122", "YYYY-MM-DDTHH:mm:ss, true").isValid()) – sawe Aug 30 '16 at 10:03
  • There's a typo in the first jsfiddle: alert(moment("2011-10-10T14:48:00", "YYYY-MM-DDTHH:mm:ss, true").isValid()) The double quote should be moved before the comma, before true. – Nick Feb 12 '18 at 09:44
1

use this to match part of your date

console.log(moment("2011-10-10T14:48:00", "YYYY-MM-DD", false).isValid())

if you want exact format match then

console.log(moment("2011-10-10T14:48:00", "YYYY-MM-DDTHH:mm:ss", true).isValid())
Cris
  • 12,799
  • 5
  • 35
  • 50