1

I have a date in format 11-May-16 and would like to create an instance of date using constructor new Date('11-May-16').

However in Internet Explorer and Firefox it is not working.

How can it be fixed?

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
josh_boaz
  • 1,963
  • 7
  • 32
  • 69

1 Answers1

2

The only formats officially supported by the Date() constructor (which calls Date.parse(...)) are IETF-compliant RFC 2822 timestamps and ISO8601.

Any other formats are implementation specific and may not be supported cross-browser.

A quick dependency-free way to create a date would be to parse it yourself. For example, using regexes:

function parseDate(date) {
  var MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', ...];
  var date = '11-May-16';
  var match = date.match(/(\d{2})-([A-Za-z]{3})-(\d{2})/);
  return new Date(2000 + parseInt(match[3]), MONTHS.indexOf(match[2]), match[1]);
}

parseDate('11-May-16')
-> Wed May 11 2016 00:00:00
Community
  • 1
  • 1
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
  • 1
    Note that the only **official** format supported is a subset of ISO 8601 (see [*EMA-262 §20.3.3.2*](http://www.ecma-international.org/ecma-262/6.0/#sec-date.parse)). – RobG May 12 '16 at 04:12