Ali Adravi
Ali Adravi

Reputation: 22723

JavaScript new Date('22/22/2222') convert to a valid date in IE 11

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?

Upvotes: 0

Views: 371

Answers (4)

aebabis
aebabis

Reputation: 3705

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");

Upvotes: 1

WouldBeNerd
WouldBeNerd

Reputation: 687

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);

Upvotes: 0

Juan Caicedo
Juan Caicedo

Reputation: 1523

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/

Upvotes: 0

James Thorpe
James Thorpe

Reputation: 32202

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.

Upvotes: 3

Related Questions