Convert a date in the format DD-MMM-YYYY in JavaScript in IE

by EvanJPalmer


So when I’m developing, I use Chrome. (Who doesn’t?)

Eventually I test in IE and fix whatever I find.

Today I found that IE doesn’t parse dates in the format dd-mmm-yyyy.

Try it:

new Date('12-Aug-2012')

I searched around for a standard solution to this, and couldn’t find one. I find this really surprising considering it’s not an uncommon date format.

So I just knocked one up myself based on this stack overflow response:

var customDateDdMmmYyyy = function (dateString) {
 var dateParts = dateString.split(/-/);
 return new Date((dateParts[2] * 1), ($.inArray(dateParts[1].toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]) * 1), dateParts[0] * 1);

My answer is different to the stack overflow for a couple of reasons:

1) I’ve changed the code to return a new date object
2) I’m not dealing with a datatable
3) I chose not to use modernizr, and just always call my custom date function

I hope someone finds this useful, but because I feel like there must be a better, more standard solution than this.

Sadly, I couldn’t find it.