0
var checkDate = new Date("22/22/2222");

When I check in IE 11 it convert to Wed Oct 22 00:00:00 EDT 2223 so my next line fails

if (checkDate != 'Invalid Date')

How to fix it?

mario ruiz
  • 880
  • 1
  • 11
  • 28
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • 1
    How is this at all convertible? What's the 22nd month of the year? – lux Apr 07 '16 at 16:29
  • Check further details for your issue here http://stackoverflow.com/questions/2182246/date-constructor-returns-nan-in-ie-but-works-in-firefox-and-chrome . Try a different way to create your date. – mario ruiz Apr 07 '16 at 16:37

4 Answers4

3

As you've passed in an invalid date format (as far as the ECMA spec is concerned), the browser is free to choose to interpret it how it wishes. It seems IE thinks it can deal with it:

The function first attempts to parse the format of the String according to the rules (including extended years) called out in Date Time String Format (20.3.1.16). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

If you're going to pass in strange formats, you're either going to need to validate them yourself or use a library that can do so better than the browsers can.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
1

Months and days can "wrap" in JavaScript. One way to test if the date is legal is to see if the output date corresponds to the original input string. If it doesn't, then it wrapped.

function check(inputString) {
  var checkDate = new Date(inputString);

  // Get month, day, and year parts, assuming
  // you don't have them already
  var arr = inputString.split('/');
  var isMonthWrapped = +arr[0] !== checkDate.getMonth() + 1;
  var isDayWrapped = +arr[1] !== checkDate.getDate();
  var isYearWrapped = +arr[2] !== checkDate.getFullYear();
  
  console.log("Parts", +arr[0], +arr[1], +arr[2]);
  console.log("Results", checkDate.getMonth() + 1, checkDate.getDate(), checkDate.getFullYear());
  console.log("Wrapped?", isMonthWrapped, isDayWrapped, isYearWrapped);

  var isLegal = checkDate !== 'Invalid Date' && !isMonthWrapped && !isDayWrapped && !isYearWrapped;
  document.body.innerHTML += inputString + ': ' + (isLegal ? 'Legal' : 'Illegal') + '<br>';
};

check("22/22/2222");
check("12/12/2222");
aebabis
  • 3,657
  • 3
  • 22
  • 40
0

I think that moment.js http://momentjs.com/ is a complete and good package about dates.

You could add string date and format.

moment("12/25/1995", "MM/DD/YYYY");

And you could check if date is valid.

moment("not a real date").isValid();

See documentation http://momentjs.com/docs/#/parsing/string-format/

Juan Caicedo
  • 1,425
  • 18
  • 31
0

You should break up your string and parse Each date to integers individually. It will be much safer.

Do something like this

var dateString = "22/22/2222";
dateString.indexOf("/");
var day = parseInt(dateString.slice(0,dateString.indexOf("/")));
dateString = dateString.slice(1+dateString.indexOf("/"), dateString.length);
var month = parseInt(dateString.slice(0,dateString.indexOf("/")))
dateString = dateString.slice(1+dateString.indexOf("/"), dateString.length);
var year = parseInt(dateString);

console.log(day, month, year);
var date = new Date(0);
if(month>12) console.log("hey this is totally not a valid month maaaan!")
date.setDate(day);
date.setMonth(month);
date.setYear(year);
console.log(date);
WouldBeNerd
  • 629
  • 11
  • 29