How to test a string is valid date or not using moment?

73,128

Solution 1

Yes, you could use momentjs to parse it and compare it back with the string

function isValidDate(str) {
  var d = moment(str,'D/M/YYYY');
  if(d == null || !d.isValid()) return false;

  return str.indexOf(d.format('D/M/YYYY')) >= 0 
      || str.indexOf(d.format('DD/MM/YYYY')) >= 0
      || str.indexOf(d.format('D/M/YY')) >= 0 
      || str.indexOf(d.format('DD/MM/YY')) >= 0;
}

Test code

tests = ['',' ','x','1/1','1/12/2015','1/12/2015 1:00 PM']
for(var z in tests) {
  var test = tests[z];
  console.log('"' + test + '" ' + isValidDate(test));
}

Output

 "" false
 " " false
 "x" false
 "1/1" false
 "1/12/2015" true
 "1/12/2015 1:00 PM" true

Solution 2

Moment has a function called isValid.

You want to use this function along with the target date format and the strict parsing parameter to true (otherwise your validation might not be consistent) to delegate to the library all the needed checks (like leap years):

var dateFormat = "DD/MM/YYYY";
moment("28/02/2011", dateFormat, true).isValid(); // return true
moment("29/02/2011", dateFormat, true).isValid(); // return false: February 29th of 2011 does not exist, because 2011 is not a leap year
Share:
73,128
Valamas
Author by

Valamas

Updated on July 09, 2022

Comments

  • Valamas
    Valamas almost 2 years

    I would like to test if a date and or time entered is valid.

    Can this be done with moment as date and time testing with javascript seems a nightmare. (have spent hours on this).

    Test data looks like this.

    Invalid

    invalid = ""
    invalid = " "
    invalid = "x"
    invalid = "1/1"
    invalid = "30/2/2015"
    invalid = "2/30/2015"
    

    Is Valid

    isvalid = "1/12/2015"
    isvalid = "1/12/2015 1:00 PM";
    

    Have tried various javascript methods with hours of trials failing.

    I thought moment would have something for this. So tried the following, all of which does not work because I do no think moment works like this.

    var valid = moment(input).isDate()
    var valid = moment().isDate(input)
    

    My time format is "dd/mm/yyyy"

  • Valamas
    Valamas over 9 years
    Date.parse('2/30/2011') returns as valid. 30th Feb or 2nd of the 30th month should be invalid.
  • Karl
    Karl over 9 years
    When including a link it's helpful to also include the relevant snippet in case the link is broken in future etc
  • Mnebuerquo
    Mnebuerquo over 8 years
    I think this answer should be the accepted answer. The isValid() function is able to handle things like leap years and such and you're not doing all the string comparisons like in the currently accepted answer.
  • pulkitsinghal
    pulkitsinghal about 8 years
    You may need to use the 3rd argument as well: As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. stackoverflow.com/questions/19978953/…
  • Avatar
    Avatar almost 8 years
    The comment above is very important, see example: var dateFormat = 'DD.MM.YYYY'; var givendate = '12'; alert(moment(givendate, dateFormat).isValid()); // true! alert(moment(givendate, dateFormat, true).isValid()); // false
  • Coder
    Coder over 7 years
    this is checking only format. 30/02/2016 is not a valid date.but moment isValid() function return true
  • Luca Fagioli
    Luca Fagioli over 7 years
    @nikhil moment("30/02/2016", "DD/MM/YYYY", true) return false. Read carefully the answer: you need to pass also the date format.
  • Coder
    Coder over 7 years
    @Luca Fagioli moment("01/02/0000", 'DD/MM/YYYY', true).isValid() return true
  • Luca Fagioli
    Luca Fagioli over 7 years
    @Nikhil "01/02/0000" is a valid date.
  • R K Sharma
    R K Sharma over 7 years
    'moment("11/25/2016 18:30","MM/dd/yyyy HH:mm",true).isValid()' is returning false. why? do I have to specify the time format also?
  • Luca Fagioli
    Luca Fagioli over 7 years
    @R K Sharma dd and yyyy are invalid tokens. You should use DD and YYYY instead. So moment("11/25/2016 18:30","MM/DD/YYYY HH:mm",true).isValid() returns true.
  • Amio.io
    Amio.io over 7 years
    Date.parse('foo-bar 2014'); returns NaN which I find better than new Date('foo-bar 2014').toString(); that returns "Invalid Date"
  • Green
    Green over 6 years
    Many date formats are also supported, Like moment('2017-09', ['YYYY-MM', 'YYYY-MM-DD', 'MM-DD-YYY'], true) momentjs.com/docs/#/parsing/string-formats