function DateFormatter(){this.date=new Date();this.mask;this.specifiers={};this.spPositions=[];this.replacers={};this.parsers={};this.abbrMonthNames=[];this.fullMonthNames=[];this.Format=function(date){this.date=date;var sp;var pos;var replacerKey;var result=this.mask;for(var i=0;i<this.spPositions.length;i++){pos=this.spPositions[i];sp=this.specifiers[pos];replacerKey=sp.substr(0,1);if(this.replacers[replacerKey]){result=result.substr(0,pos)+this.replacers[replacerKey].call(this,sp.length)+result.substr(pos+sp.length);}}return result;};this.Parse=function(str){this.date=new Date();this.strToParse=str;this.catchNumbers(str);var parserKey;var sp;var pos;var parseResult;var error=false;for(var i=0;i<this.spPositions.length;i++){pos=this.spPositions[i];sp=this.specifiers[pos];parserKey=sp.substr(0,1);if(this.parsers[parserKey]){parseResult=this.parsers[parserKey].call(this,sp.length);if(!parseResult){error=true;break;}}}if(error)this.date=false;return this.date;};this.SetFormatString=function(mask){this.specifiers={};this.spPositions=[];this.mask=mask;var sp=["d","M","y"];for(var i in sp){var regex=new RegExp(sp[i]+"+","g");var match;do{match=regex.exec(mask);if(match){this.spPositions.push(match.index);this.specifiers[match.index]=match.toString();}}while(match);}
this.spPositions.sort(function(a,b){return b-a;});};this.replaceDay=function(length){var value=this.date.getDate();if(length==2&&value<10)value="0"+value.toString();return value.toString();};this.replaceMonth=function(length){var value=1+this.date.getMonth();switch(length){case 1:return value.toString();case 2:return value<10?"0"+value.toString():value.toString();case 3:return this.abbrMonthNames[value-1];default:return this.fullMonthNames[value-1];}};this.replaceYear=function(length){var value=this.date.getFullYear();if(length==1){if(value<10)return value.toString();length=2;}
value=value.toString();while(value.length<length)value="0"+value;if(value.length>length)value=value.substr(value.length-length,length);return value;};this.replacers={"d":this.replaceDay,"M":this.replaceMonth,"y":this.replaceYear};this.catchNumbers=function(str){this.parseNumbers=[];var regex=/\d+/g;var match;do{match=regex.exec(str);if(match)this.parseNumbers.push(match[0]);}while(match);this.currentParseNumber=this.parseNumbers.length-1;};this.popParseNumber=function(){return this.parseNumbers[this.currentParseNumber--];};this.findAbbrMonth=function(){return this.findMonthCore(this.abbrMonthNames);};this.findFullMonth=function(){return this.findMonthCore(this.fullMonthNames);};this.findMonthCore=function(monthNames){for(var i in monthNames)if(this.strToParse.indexOf(monthNames[i])>-1)return 1+parseInt(i);return false;};this.parseDay=function(length){var value=this.parseDecInt(this.popParseNumber());if(isNaN(value)||value<1||value>31)return false;this.date.setDate(value);return true;};this.parseMonth=function(length){var value;switch(length){case 1:case 2:value=this.parseDecInt(this.popParseNumber());break;case 3:value=this.findAbbrMonth();break;default:value=this.findFullMonth();break;}if(isNaN(value)||value<1||value>12)return false;this.date.setMonth(value-1);return true;};this.parseYear=function(length){var value=this.parseDecInt(this.popParseNumber());if(isNaN(value)||value<1||value>9999)return false;if(length<3)this.date.setYear(value);else{if(value<100)value=100;this.date.setFullYear(value);}return true;};this.parsers={"d":this.parseDay,"M":this.parseMonth,"y":this.parseYear};this.parseDecInt=function(str){return parseInt(str,10);};} 