js 得到两个时间之间为几个月和几天

/**
 * 操作日期
 * @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;
 } ;


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值