Skip to content

Instantly share code, notes, and snippets.

@veltman
Last active August 29, 2015 13:57
Show Gist options
  • Save veltman/9500968 to your computer and use it in GitHub Desktop.
Save veltman/9500968 to your computer and use it in GitHub Desktop.
Bad.js for @brittanystoroz, circa 2007
function fixDate(fieldname,datestr) {
//Make lowercase
newdatestr = datestr.toLowerCase();
//trim first
newdatestr = newdatestr.replace(/^\s+|\s+$/g,'');
//If it was just whitespace, don't run the function
if (!newdatestr.length) {
return;
}
//replace month names and abbreviations first
newdatestr = newdatestr.replace('january','1');
newdatestr = newdatestr.replace('jan','1');
newdatestr = newdatestr.replace('jan.','1');
newdatestr = newdatestr.replace('february','2');
newdatestr = newdatestr.replace('feb','2');
newdatestr = newdatestr.replace('feb.','2');
newdatestr = newdatestr.replace('march','3');
newdatestr = newdatestr.replace('mar','3');
newdatestr = newdatestr.replace('mar.','3');
newdatestr = newdatestr.replace('april','4');
newdatestr = newdatestr.replace('apr','4');
newdatestr = newdatestr.replace('apr.','4');
newdatestr = newdatestr.replace('may','5');
newdatestr = newdatestr.replace('may.','5');
newdatestr = newdatestr.replace('june','6');
newdatestr = newdatestr.replace('jun','6');
newdatestr = newdatestr.replace('jun.','6');
newdatestr = newdatestr.replace('july','7');
newdatestr = newdatestr.replace('jul','7');
newdatestr = newdatestr.replace('jul.','7');
newdatestr = newdatestr.replace('august','8');
newdatestr = newdatestr.replace('aug','8');
newdatestr = newdatestr.replace('aug.','8');
newdatestr = newdatestr.replace('september','9');
newdatestr = newdatestr.replace('sept.','9');
newdatestr = newdatestr.replace('sept','9');
newdatestr = newdatestr.replace('sep.','9');
newdatestr = newdatestr.replace('sep','9');
newdatestr = newdatestr.replace('october','10');
newdatestr = newdatestr.replace('oct','10');
newdatestr = newdatestr.replace('oct.','10');
newdatestr = newdatestr.replace('november','11');
newdatestr = newdatestr.replace('nov.','11');
newdatestr = newdatestr.replace('nov','11');
newdatestr = newdatestr.replace('december','12');
newdatestr = newdatestr.replace('dec','12');
newdatestr = newdatestr.replace('dec.','12');
//replace slashes, periods, dashes, spaces, commas with slashes
newdatestr = newdatestr.replace(/[.]/g,'/');
newdatestr = newdatestr.replace(/[-]/g,'/');
newdatestr = newdatestr.replace(/[,]/g,'/');
newdatestr = newdatestr.replace(/\s/g,'/');
//if there are any non numeric, non slash characters now, exit the function
//FILL IN
//remove double,triple slashes
newdatestr = newdatestr.replace('///////','/');
newdatestr = newdatestr.replace('/////','/');
newdatestr = newdatestr.replace('////','/');
newdatestr = newdatestr.replace('///','/');
newdatestr = newdatestr.replace('//','/');
//remove leading zero from each
componentsplit = newdatestr.split('/');
for (i = 0; i < componentsplit.length - 1; i++) {
while (componentsplit[i].substr(0,1) == '0') {
componentsplit[i] = componentsplit[i].substr(1,9999);
}
}
//if the year is two digits and is after 2010, preface with 19, otherwise preface with 20
if (componentsplit[componentsplit.length-1].length == 2) {
if (parseInt(componentsplit[componentsplit.length-1]) > 10) {
componentsplit[componentsplit.length-1] = '19'+componentsplit[componentsplit.length-1];
} else {
componentsplit[componentsplit.length-1] = '20'+componentsplit[componentsplit.length-1];
}
}
newdatestr = componentsplit.join('/');
//should be either a four digit year, a one-two digit month + / + four digit year, or a one-two digit month + / + 1-2 digit day + / + four digit year
if (/^([0-9]{1,2}\/){0,2}[0-9]{4}$/.test(newdatestr)) {
document.getElementById(fieldname).value = newdatestr;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment