
Date.prototype.getAMPMHours = function(){
	var hours = this.getHours();
	if(hours > 12) hours -= 12;
	if(hours == 0) hours = 12;
	return hours;
}

Date.prototype.getAMPM = function(){
	return (this.getHours() < 12 ? "AM" : "PM");
}

Date.prototype.toId = function(){
	var month = this.getMonth() + 1;
	var year = this.getFullYear();
	var day = this.getDate();
	return month + '-' + day + '-' + year;
}

Date.prototype.isLeapYear = function(){
	var yr = this.getFullYear();
	var lp = new Date(yr,1,29);
	return (lp.getMonth()==1);
}
Date.prototype.getHumanDateForDay = function(){
	return this.getDayName(true) + ", " + this.getMonthName(true) + " " + this.getDate();
}

Date.prototype.getHumanDate = function(){
	return this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear();
}

Date.prototype.getHumanTime = function(){
	var hour = this.getHours();
	var minutes = this.getMinutes().toString();
	
	if(minutes.length == 1)
		minutes = "0" + minutes;

	var suffix = (hour < 12 ? "am" : "pm");
	hour = (hour < 12 ? hour : hour - 12);
	if(hour == 0 && minutes == "00"){
		return (suffix == "am" ? "Midnight" : "Noon");
	}else{
		return hour + ":" + minutes + " " + suffix;
	}
	
}

Date.prototype.getFirstDayOffset = function(){
	var d = new Date(this.getFullYear,0,1);
	var offset = 0;
	while (d.getDay() != 0){
		offset++;
		d = new Date(this.getFullYear(),0,1 - offset);
	}
	return offset;
}

Date.prototype.getFirstOfWeek = function(){
	return (new Date(this.getFullYear(), this.getMonth(), this.getDate()-this.getDay()));
}

Date.prototype.getMonthDatesSolid = function(){
	var month = new Date(this.getFullYear(), this.getMonth(), 1);
	var prevMonth = new Date(this.getFullYear(), this.getMonth()-1, 1);
	var nextMonth = new Date(this.getFullYear(), this.getMonth()+1, 1);
	
	var startingDay = month.getDay();
	var endingDay = (month.getLastMonthDate()).getDay();
	
	var MonthDates = [];
	for(var i = startingDay - 1; i >= 0; i--) {
		MonthDates.push(new Date(prevMonth.getFullYear(),prevMonth.getMonth(),prevMonth.getDaysInMonth() - i));
	}
	for(var i = 1; i <= month.getDaysInMonth(); i++){
		MonthDates.push(new Date(month.getFullYear(),month.getMonth(), i));
	}
	for(var i = endingDay + 1; i <= 6; i++) {
		MonthDates.push(new Date(nextMonth.getFullYear(),nextMonth.getMonth(),i - endingDay));
	}
	return MonthDates;
}

Date.prototype.getDaysInMonth = function(){
	var dmn = [31,(this.isLeapYear() ? 29 : 28),31,30,31,30,31,31,30,31,30,31];
	return dmn[this.getMonth()];
}

Date.prototype.getLastMonthDate = function(){
	return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth());
}

Date.prototype.getNextDay = function(){
	return new Date(this.getFullYear(),this.getMonth(),this.getDate() + 1);
}

Date.prototype.getPreviousDay = function(){
	return new Date(this.getFullYear(),this.getMonth(),this.getDate() - 1);
}

Date.prototype.getWeekNumber = function(){
	var firstSundayOffset = this.getFirstDayOffset();
	
	var m = this.getMonth();
	var d = this.getDate();
	var wn = 0;
	var i = 0;
	while(i<m){
		wn += DATE_MONTH_NUMBERS[i];
		i++;
	}
	wn += d;
	if(wn < firstSundayOffset){
		return 0
	} else {
		return Math.ceil(wn / 7);		
	}

}

Number.prototype.getDateFromWeekNumber = function(year){
	if(isUnset(year)) year = (new Date).getFullYear();
	return new Date(year,0,(this * 7) - (new Date).getFirstDayOffset() + 1);
}

Date.prototype.getMonthName = function(short){
	var names = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var shortNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	
	return (short ? shortNames[this.getMonth()] : names[this.getMonth()]);
}

Date.prototype.getDayName = function(short){
	var names = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
	var shortNames = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
	return (short ? shortNames[this.getDay()] : names[this.getDay()]).toString();
}

Date.prototype.getDatabaseDate = function(short){
	if(short != true) short = false;
	return this.getFullYear() + "-" + (this.getMonth() + 1).padDateInteger() + "-" + this.getDate().padDateInteger() + (short ? "" : " " + this.getHours().padDateInteger() + ":" + this.getMinutes().padDateInteger());
}

// Date.prototype.upTo = function(endDate){
// 	var startDate = new Date(this.getFullYear(), this.getMonth(), this.getDate());
// 	endDate = new Date(endDate.getFullYear(), endDate.getMonth(), this.getDate());
// 	for(var i = startDate.getTime(); i <= endDate.getTime(); i = i + 2){
// 		console.log(i);
// 	}
// }

Number.prototype.padDateInteger = function(){
	var s = String(this);
	if(s.length == 1)
		s = "0" + s;
	return s;
}



var DATE_DATABASE_PARSE = /^(\d+)-(\d+)-(\d+) (\d+):(\d+)$/;
String.prototype.getDateFromDatabase = function(){
	var d = DATE_DATABASE_PARSE.exec(this);
	if(d.length == 6)
		return new Date(d[1],d[2] - 1,d[3],d[4],d[5]);
	else
		return null;
}


var DATE_DAY_NAMES = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var DATE_MONTH_NUMBERS = [31,((new Date).isLeapYear() ? 29 : 28),31,30,31,30,31,31,30,31,30,31];