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?
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?
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