function setLocaleText(){
return {"EN_MONTH_NAMES":[
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
}
}
function jsParseDate(formattedDateString) {
var date = formattedDateString;
var year = date.match(/(19|20)\d\d$/);
if (year) {
year = year[0];
}
var month = null;
var monthString = date.match(/[a-zA-Z]{3,5}/i);
if (monthString) {
var dateObj = setLocaleText();
month = dateObj['EN_MONTH_NAMES'].indexOf(monthString[0].charAt(0).toUpperCase() + monthString[0].slice(1).toLowerCase());
if (month === -1) {
month = dateObj['MONTH_NAMES'].indexOf(monthString[0]);
}
}
var day = date.match(/^\d\d?/);
if (day) {
day = day[0];
}
if (year && month && day) {
return new Date(year, month, day);
} else if (year && month) {
return new Date(year, month);
} else {
return new Date(year);
}
}
console.assert(jsParseDate('20MAR1956')+"" == new Date(1956,02,20)+"", 'failed with 20MAR1956' )
console.assert(jsParseDate('20Mar1956')+"" == new Date(1956,02,20)+"", 'failed with 20Mar1956' )
console.assert(jsParseDate('20/Mar/1956')+"" == new Date(1956,02,20)+"", 'failed with 20/Mar/1956')
console.assert(jsParseDate('20/mar/1956')+"" == new Date(1956,02,20)+"", 'failed with 20/mar/1956' )
console.assert(jsParseDate('20/MAR/1956')+"" == new Date(1956,02,20)+"", 'failed with 20/MAR/1956' )
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.