// JavaScript Document

Date.prototype.customFormat=function(formatString){ 
   var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,dMod,th;
   YY = ((YYYY=this.getFullYear())+"").substr(2,2);
   MM = (M=this.getMonth()+1)<10?('0'+M):M;
   MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substr(0,3);
   DD = (D=this.getDate())<10?('0'+D):D;
   DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substr(0,3);
   th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
   formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);

   h=(hhh=this.getHours());
   if (h==0) h=24;
   if (h>12) h-=12;
   hh = h<10?('0'+h):h;
   ampm=hhh<12?'am':'pm';
   mm=(m=this.getMinutes())<10?('0'+m):m;
   ss=(s=this.getSeconds())<10?('0'+s):s;
   return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm);
}

Date.prototype.toCustomString = function ( formatString ) {
	/*
		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
	*/
	
	formatString = ' '+formatString;
	
	var regex = [{s:/([^\\])Y/g, r:'$1#YYYY#'},
				 {s:/([^\\])y/g, r:'$1#YY#'},
				 {s:/([^\\])M/g, r:'$1#MMM#'},
				 {s:/([^\\])F/g, r:'$1#MMMM#'},
				 {s:/([^\\])m/g, r:'$1#MM#'},
				 {s:/([^\\])n/g, r:'$1#M#'},
				 {s:/([^\\])d/g, r:'$1#dd#'},
				 {s:/([^\\])j/g, r:'$1#d#'},
				 {s:/([^\\])D/g, r:'$1#DDD#'},
				 {s:/([^\\])l/g, r:'$1#DDDD#'},
				 {s:/([^\\])h/g, r:'$1#hh#'},
				 {s:/([^\\])S/g, r:'$1#th#'},
				 {s:/([^\\])H/g, r:'$1#HH#'},
				 {s:/([^\\])i/g, r:'$1#mm#'},
				 {s:/([^\\])s/g, r:'$1#ss#'},
				 {s:/([^\\])A/g, r:'$1#AP#'},
				 {s:/([^\\])a/g, r:'$1#ap#'}]; 
	
	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;
	
	for ( var i=0; i<regex.length; 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,'');
}