Check if date is a valid one

JavascriptDateDatetimeMomentjs

Javascript Problem Overview


Following is the scenario:

I have a String date and a date format which is different. Ex.:
date: 2016-10-19
dateFormat: "DD-MM-YYYY".

I need to check if this date is a valid date.

I have tried following things

var d = moment("2016-10-19",dateFormat);

d.isValid() is returning false every time. Does not Moment.js parse the date in the given format?

Then I tried to format the date in DD-MM-YYYY first and then pass it to Moment.js:

var d = moment("2016-10-19").format(dateFormat);
var date = moment(d, dateFormat);

Now date.isValid() is giving me the desired result, but here the Moment.js date object is created twice. How can I avoid this? Is there a better solution?

FYI I am not allowed to change the dateFormat.

Javascript Solutions


Solution 1 - Javascript

Was able to find the solution. Since the date I am getting is in ISO format, only providing date to moment will validate it, no need to pass the dateFormat.

var date = moment("2016-10-19");

And then date.isValid() gives desired result.

Solution 2 - Javascript

var date = moment('2016-10-19', 'DD-MM-YYYY', true);

You should add a third argument when invoking moment that enforces strict parsing. Here is the relevant portion of the moment documentation http://momentjs.com/docs/#/parsing/string-format/ It is near the end of the section.

Solution 3 - Javascript

I just found a really messed up case.

moment('Decimal128', 'YYYY-MM-DD').isValid() // true

Solution 4 - Javascript

Here you go: Working Fidddle

$(function(){
  var dateFormat = 'DD-MM-YYYY';
  alert(moment(moment("2012-10-19").format(dateFormat),dateFormat,true).isValid());
});

Solution 5 - Javascript

How to check if a string is a valid date using Moment, when the date and date format are different

Sorry, but did any of the given solutions on this thread actually answer the question that was asked?

> I have a String date and a date format which is different. Ex.: date: 2016-10-19 dateFormat: "DD-MM-YYYY". I need to check if this date is a valid date.

The following works for me...

const date = '2016-10-19';
const dateFormat = 'DD-MM-YYYY';
const toDateFormat = moment(new Date(date)).format(dateFormat);
moment(toDateFormat, dateFormat, true).isValid();

// Note I: `new Date()` circumvents the warning that
//   Moment throws (https://momentjs.com/guides/#/warnings/js-date/),
//   but may not be optimal.

// Note II: passing `true` as third argument to `moment()` enables strict-mode
//   https://momentjs.com/guides/#/parsing/strict-mode/

But honestly, don't understand why moment.isDate()(as documented) only accepts an object. Should also support a string in my opinion.

Solution 6 - Javascript

I use moment along with new Date to handle cases of undefined data values:

const date = moment(new Date("2016-10-19"));

because of: moment(undefined).isValid() == true

where as the better way: moment(new Date(undefined)).isValid() == false

Solution 7 - Javascript

console.log(` moment('2019-09-01', 'YYYY-MM-DD').isValid()?  ` +moment('2019-09-01', 'YYYY-MM-DD').isValid())
console.log(` moment('2019-22-01', 'YYYY-DD-MM').isValid()?  ` +moment('2019-22-01', 'YYYY-DD-MM').isValid())
console.log(` moment('2019-22-22', 'YYYY-DD-MM').isValid()?  ` +moment('2019-22-22', 'YYYY-DD-MM').isValid())
console.log(` moment('undefined', 'YYYY-DD-MM').isValid()?  ` +moment('undefined', 'YYYY-DD-MM').isValid())

 moment('2019-09-01', 'YYYY-MM-DD').isValid()?  true
 moment('2019-22-01', 'YYYY-DD-MM').isValid()?  true
 moment('2019-22-22', 'YYYY-DD-MM').isValid()?  false
 moment('undefined', 'YYYY-DD-MM').isValid()?  false

Solution 8 - Javascript

If the date is valid then the getTime() will always be equal to itself.

var date = new Date('2019-12-12');
if(date.getTime() - date.getTime() === 0) {
	console.log('Date is valid');
} else {
	console.log('Date is invalid');
}

Solution 9 - Javascript

What I have used is moment.js

let value = '19-05-1994';
let check = moment(value,'DD-MM-YYYY', true).isValid();
        console.log(check) //returns true
        return check

Solution 10 - Javascript

You can use

var d = moment("2016-10-19",dateFormat);
if(!isNaN(d)) {
  // d is a valid date.
}

Solution 11 - Javascript

Try this one. It is not nice but it will work as long as the input is constant format from your date picker.

It is badDate coming from your picker in this example

https://jsfiddle.net/xs8tvox9/

var dateFormat = 'DD-MM-YYYY'
var badDate = "2016-10-19";

var splittedDate = badDate.split('-');

if (splittedDate.length == 3) {
  var d = moment(splittedDate[2]+"-"+splittedDate[1]+"-"+splittedDate[0], dateFormat);
  alert(d.isValid())
} else {
  //incorrectFormat
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaneshView Question on Stackoverflow
Solution 1 - JavascriptGaneshView Answer on Stackoverflow
Solution 2 - JavascriptAndrewView Answer on Stackoverflow
Solution 3 - JavascriptSam AraizaView Answer on Stackoverflow
Solution 4 - JavascriptgschambialView Answer on Stackoverflow
Solution 5 - Javascripttim-montagueView Answer on Stackoverflow
Solution 6 - JavascriptwakeupojView Answer on Stackoverflow
Solution 7 - JavascriptSandeView Answer on Stackoverflow
Solution 8 - JavascriptTanzeem BhattiView Answer on Stackoverflow
Solution 9 - JavascriptoluwatysonView Answer on Stackoverflow
Solution 10 - JavascriptErisan OlasheniView Answer on Stackoverflow
Solution 11 - JavascriptPochenView Answer on Stackoverflow