日期处理通用函数

var DateUtils_MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
/** 时间处理公共函数 */
function DateUtils() {}

DateUtils.prototype.toDate = function(strDate) {
	if (strDate.length == 19) {	// YYYY-MM-DD HH:MI:SS
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		var date  = strDate.substring(8, 10);
		var hour  = strDate.substring(11, 13);
		var min   = strDate.substring(14, 16);
		var sec   = strDate.substring(17, 19);
		return new Date(year, month - 1, date, hour, min, sec);
	} else if (strDate.length == 10) { // "YYYY-MM-DD"
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		var date  = strDate.substring(8, 10);
		return new Date(year, month - 1, date);
	} else if (strDate.length == 7) { // "YYYY-MM"
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		return new Date(year, month - 1);
	} else if (strDate.length == 4) { // "YYYY"
		var year  = strDate.substring(0, 4);
		return new Date(year);		
	} else {
		alert("DateUtils.toDate(strDate) error! invalid argument(format).");
	}
}

DateUtils.prototype.toString = function(date, format) {
	var strDate;
	var year  = date.getFullYear();
	var month = date.getMonth() + 1;
	var day   = date.getDate();
	var hour  = date.getHours();
	var min   = date.getMinutes();
	var sec   = date.getSeconds();
	month = (parseInt(month) < 10) ? ("0" + month) : (month);
	day   = (parseInt(day)   < 10) ? ("0" + day )  : (day);
	hour  = (parseInt(hour)  < 10) ? ("0" + hour)  : (hour);
	min   = (parseInt(min)   < 10) ? ("0" + min)   : (min);
	sec   = (parseInt(sec)   < 10) ? ("0" + sec)   : (sec);
	if ("YYYY-MM-DD HH:MI:SS" == format) {
		strDate = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
	} else if ("YYYY-MM-DD" == format) {
		strDate = year + "-" + month + "-" + day;
	} else if ("YYYY-MM" == format) {
		strDate = year + "-" + month;	
	} else if ("YYYY" == format) {
		strDate = year;		
	} else {
		alert("DateUtils.toString(date, format) error! invalid argument(format).");
	}
	return strDate;
}

DateUtils.prototype.getMonthDays = function(date,month) {
	var year = date.getFullYear();
	if (typeof month == "undefined") {
		month = date.getMonth();
	}
	if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
		return 29;
	} else {
		return DateUtils_MD[month];
	}
};
	
DateUtils.prototype.addDays = function(dayOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
	date = new Date(date.getTime() + parseInt(dayOffset) * 24 * 3600 * 1000);
	return this.toString(new Date(date), "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addMonths = function(monthOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()): this.toDate(strBaseDate);
	var month=date.getMonth();
	var cd=date.getDate();//this.getMonthDays(date,month);
	var td=this.getMonthDays(date,date.getMonth() + parseInt(monthOffset));
	if(cd > td){date.setDate(td);}
	date.setMonth(date.getMonth() + parseInt(monthOffset));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addMonthsForStart = function(monthOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addMonths(monthOffset, strDate);
	return this.firstDayOfMonth(strDate);
}

DateUtils.prototype.addMonthsForEnd = function(monthOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addMonths(monthOffset, strDate);
	return this.addDays(-1, this.firstDayOfMonth(strDate));
}

DateUtils.prototype.addYears = function(yearOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
	date.setYear(date.getYear() + parseInt(yearOffset));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addYearsForStart = function(yearOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addYears(yearOffset, strDate);
	return this.firstDayOfYear(strDate);
}

DateUtils.prototype.addYearsForEnd = function(yearOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addYears(yearOffset, strDate);
	return this.firstDayOfYear(strDate);
}

DateUtils.prototype.sunOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay()) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");	
}

DateUtils.prototype.monOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 1) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.tueOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 2) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.wedOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 3) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.turOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 4) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.friOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 5) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.satOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 6) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.firstDayOfMonth = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setDate(1);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.lastDayOfMonth = function(strDate) {
	strDate = (arguments.length == 0) ? this.today() : (strDate);
	strDate = this.addMonths(1, strDate);
	strDate = this.firstDayOfMonth(strDate);
	strDate = this.addDays(-1, strDate);
	return strDate;
}

DateUtils.prototype.firstDayOfYear = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setMonth(0);
	date.setDate(1);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.lastDayOfYear = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setMonth(11);
	date.setDate(31);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.today = function(format) {
	if(getToday && typeof(getToday)=="function"){
		return getToday();
	}else{
		if (arguments.length == 0) {
			return this.toString(new Date(), "YYYY-MM-DD");
		} else {
			return this.toString(new Date(), format);
		}
	}
}

你可能感兴趣的:(prototype)