// Originally from ht tp://jacwright.com/projects/javascript/date_format
// Modified to conform with strftime() instead of date()

Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (i - 1 >= 0 && format.charAt(i - 1) == "\\") { 
			returnStr += curChar;
		}
		else if (i - 1 >= 0 && format.charAt(i - 1) == "%" && replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else if (curChar != "\\" && curChar != "%"){
			returnStr += curChar;
		}
	}
	return returnStr;
};
 
Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	a: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	A: function() { return Date.replaceChars.longDays[this.getDay()]; },
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	e: function() { return (this.getDate() < 10 ? ' ' : '') + this.getDate(); },
	j: function() { var d = new Date(this.getFullYear(),0,1); return Math.ceil((this - d) / 86400000); }, // Fixed now
	u: function() { return this.getDay() + 1; },
	w: function() { return this.getDay(); },
	// Week
	U: function() { return "Not Yet Supported"; },
	V: function() { return "Not Yet Supported"; },
	W: function() { var d = new Date(this.getFullYear(), 0, 1); return Math.ceil((((this - d) / 86400000) + d.getDay() + 1) / 7); }, // Fixed now
	// Month
	b: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	B: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	h: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	// Year
	C: function() { return ('' + parseInt(this.getFullYear()).substr(0,2))-1; },
	g: function() { return "Not Yet Supported"; },
	G: function() { return "Not Yet Supported"; },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	Y: function() { return this.getFullYear(); },
	// Time
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	I: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	l: function() { return ((this.getHours() % 12 || 12) < 10 ? ' ' : '') + (this.getHours() % 12 || 12); },
	M: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	P: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	p: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	r: function() { return this.format("%I:%M:%S %p"); }, 
	R: function() { return this.format("%H:%M"); }, 
	S: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	T: function() { return this.format("%H:%M:%S"); }, 
	X: function() { return this.toString(); },
	z: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	
	// Time and Date Stamps
	c: function() { return "Not Yet Supported"; },
	D: function() { return this.format("%m/%d/%y"); }, 
	F: function() { return this.format("%Y-%m-%d"); }, 
	s: function() { return this.getTime() / 1000; },
	x: function() { return "Not Yet Supported"; },
	
	// Miscellaneous
	n: function() { return "\n"; },
	t: function() { return "\t"; }
	//'%': function() { return "%"; }
};
