
/**
 * year  : Y=1969 | y=69
 * month : F=January | M=Jan | m=01 | n=1
 * day   : d=01 | j=1 | l=Sunday | D=Sun | S=st,nd,rd,th
 * time  : H=24 hour | h=12 hour | i=min w/ zero | s=sec w/ zero | a=am,pm | A=AM,PM
 */
Date.prototype.toCustomString = function ( formatString ) {
	formatString = ' '+formatString;
	
	var regex = [{s:/([^\\])a/g, r:'$1#ap#'},{s:/([^\\])A/g, r:'$1#AP#'},{s:/([^\\])s/g, r:'$1#ss#'},{s:/([^\\])i/g, r:'$1#mm#'},{s:/([^\\])H/g, r:'$1#HH#'},{s:/([^\\])S/g, r:'$1#th#'},{s:/([^\\])h/g, r:'$1#hh#'},{s:/([^\\])l/g, r:'$1#DDDD#'},{s:/([^\\])D/g, r:'$1#DDD#'},{s:/([^\\])j/g, r:'$1#d#'},{s:/([^\\])d/g, r:'$1#dd#'},{s:/([^\\])n/g, r:'$1#M#'},{s:/([^\\])m/g, r:'$1#MM#'},{s:/([^\\])F/g, r:'$1#MMMM#'},{s:/([^\\])M/g, r:'$1#MMM#'},{s:/([^\\])y/g, r:'$1#YY#'},{s:/([^\\])Y/g, r:'$1#YYYY#'}];
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	var days   = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	var dMod, day, month, hour, minute, second, ampm, m;
	var i=regex.length;
	while ( i-- ) formatString = formatString.replace(regex[i].s,regex[i].r);
	
	m = (this.getMonth()==0)?11:(this.getMonth()-1);
	
	formatString = formatString.replace(/#YYYY#/g,this.getFullYear());
	formatString = formatString.replace(/#YY#/g,(this.getFullYear()+"").substr(2,2));
	formatString = formatString.replace(/#MMMM#/g,months[m]);
	formatString = formatString.replace(/#MMM#/g,months[m].substr(0,3));
	formatString = formatString.replace(/#MM#/g,((month=this.getMonth()+1)<10?('0'+month):month));
	formatString = formatString.replace(/#M#/g,(this.getMonth()+1));
	
	formatString = formatString.replace(/#dd#/g,((day=this.getDate())<10?('0'+day):day));
	formatString = formatString.replace(/#d#/g,this.getDate());
	formatString = formatString.replace(/#DDDD#/g,days[this.getDay()]);
	formatString = formatString.replace(/#DDD#/g,days[this.getDay()].substr(0,3));
	formatString = formatString.replace(/#th#/g,((day>10&&day<=20)?'th':((dMod=day%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th'));
	
	formatString = formatString.replace(/#HH#/g,this.getHours());
	formatString = formatString.replace(/#hh#/g,((hour=this.getHours())>12?(hour-12):hour));
	formatString = formatString.replace(/#mm#/g,((minute=this.getMinutes())<10?('0'+minute):minute));
	formatString = formatString.replace(/#ss#/g,((second=this.getMinutes())<10?('0'+second):second));
	ampm = this.getHours()<12?'am':'pm';
	formatString = formatString.replace(/#ap#/g,ampm);
	formatString = formatString.replace(/#AP#/g,ampm.toUpperCase());
	
	return formatString.substr(1).replace(/\\/g,'');
}
