/*
Date Format 0.4.1
(c) Steven Levithan <http://stevenlevithan.com>
MIT license
*/

Date.prototype.format = function () {
	var token = /"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LlZ])\b/g,
		timeZone = /\b(?:(?:(?:Pacific|Mountain|Central|Eastern|Atlantic) |[PMCEA])(?:(?:Standard|Daylight|Prevailing) |[SDP])T(?:ime)?|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		extraChars = /[^-+A-Z\d]/g,
		pad = function (num, length) {
			num = String(num);
			length = length || 2;
			while (num.length < length) num = "0" + num;
			return num;
		};

	// The module pattern is used here to cache the regexes and other private vars
	return function (mask) {
		var d = this;
		return mask.replace(token, function ($0) {
			switch ($0) {
				case "d":	return d.getDate();
				case "dd":	return pad(d.getDate());
				case "ddd":	return ["Sun","Mon","Tue","Wed","Thr","Fri","Sat"][d.getDay()];
				case "dddd":	return ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][d.getDay()];
				case "m":	return d.getMonth() + 1;
				case "mm":	return pad(d.getMonth() + 1);
				case "mmm":	return ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][d.getMonth()];
				case "mmmm":	return ["January","February","March","April","May","June","July","August","September","October","November","December"][d.getMonth()];
				case "yy":	return String(d.getFullYear()).slice(2);
				case "yyyy":	return d.getFullYear();
				case "h":	return d.getHours() % 12 || 12;
				case "hh":	return pad(d.getHours() % 12 || 12);
				case "H":	return d.getHours();
				case "HH":	return pad(d.getHours());
				case "M":	return d.getMinutes();
				case "MM":	return pad(d.getMinutes());
				case "s":	return d.getSeconds();
				case "ss":	return pad(d.getSeconds());
				case "l":	return pad(d.getMilliseconds(), 3);
				case "L":
					var m = d.getMilliseconds();
					if (m > 99) m = Math.round(m / 10);
					return pad(m);
				case "t":	return d.getHours() < 12 ? "a"  : "p";
				case "tt":	return d.getHours() < 12 ? "am" : "pm";
				case "T":	return d.getHours() < 12 ? "A"  : "P";
				case "TT":	return d.getHours() < 12 ? "AM" : "PM";
				// Time zone handling supports at least en-US versions of IE, Firefox, Safari
				case "Z":	return (String(d).match(timeZone) || [""]).pop().replace(extraChars, "");
				// Return quoted strings with the surrounding quotes removed
				default:	return $0.slice(1, $0.length - 1);
			}
		});
	};
}();