/**
* 操作日期
* @author 曹飞龙
*/
/**
* 得到两个时间之间为几个月和几天 2013-8-31 2013-10-5
*/
function getMonthAndDay(startdate, enddate) {
var month = 0;
var day = 0;
if (enddate.getDate() >= startdate.getDate()) {
month = (enddate.getFullYear() - startdate.getFullYear()) * 12
+ enddate.getMonth() - startdate.getMonth();
day = enddate.getDate() - startdate.getDate();
} else {
if (startdate.getDate() == getlastDay(startdate)) {
if (enddate.getDate() == getlastDay(enddate)) {
month = enddate.getFullYear() - startdate.getFullYear() * 12
+ enddate.getMonth() - startdate.getMonth();
day = 0;
} else {
month = enddate.getFullYear() - startdate.getFullYear() * 12
+ enddate.getMonth() - startdate.getMonth() - 1;
day = startdate.getDate();
}
} else {
if (enddate.getDate() == getlastDay(enddate)) {
month = enddate.getFullYear() - startdate.getFullYear() * 12
+ enddate.getMonth() - startdate.getMonth();
day = 0;
} else {
month = enddate.getFullYear() - startdate.getFullYear() * 12
+ enddate.getMonth() - startdate.getMonth() - 1;
var lastmonthday = getlastmonthDat(enddate);
day = lastmonthday - startdate.getDate() + enddate.getDate();
}
}
}
return {
"mounth" : month,
"day" : day
};
}
/**
* 得到上一个月有多少天
*/
function getlastmonthDat(date) {
date.setDate(1);
date.setDate(date.getDate() - 1);
return date.getDate();
}
/**
* 得到date 所在的月有多少天
*
* @param date
*/
function getlastDay(date) {
date.setMonth(date.getMonth() + 1);
date.setDate(1);
date.setDate(date.getDate() - 1);
return date.getDate();
}
/** * 时间格式化
*
* @param format
* @returns
*/
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, // month
"d+" : this.getDate(), // day
"h+" : this.getHours(), // hour
"m+" : this.getMinutes(), // minute
"s+" : this.getSeconds(), // second
"q+" : Math.floor((this.getMonth()+3)/3), // quarter
"S" : this.getMilliseconds() // millisecond
};
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
} ;