/**
 * 千里及
 * 2021.2.3
 * 处理日期的对象
 */
export const dateHandle = {
	/**
	 * 获取今天的日期时间串
	 */
	todayDate: function() {
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		return year + "-" + dateHandle.add0(month) + "-" + dateHandle.add0(day);
	},
	/**
	 * 计算两个日期之间相差的天数
	 * @param {Object} sDate1
	 * @param {Object} sDate2
	 */
	dateDiff: function(sDate1, sDate2) {
		if(sDate1 == sDate2){
			return 0;
		}
		if(!sDate1){
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth()+1;
			var day = date.getDate();
			sDate1 = year + "-" + month + "-" + day;
		}
		var oDate1, oDate2, iDays ; 
		oDate1 = dateHandle.createDate(sDate1);
		oDate2 = dateHandle.createDate(sDate2);
		//把相差的毫秒数转换为天数  
		iDays  =  parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24);
		return  iDays;  
	},
	/**
	 * 计算指定日期之后X天数的日期
	 * @param {Object} dateTemp
	 * @param {Object} days
	 */
	getNewData: function(dateTemp, days) {  
		var nDate = dateHandle.createDate(dateTemp);
		var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);  
		var rDate = new Date(millSeconds);
		var year = rDate.getFullYear();  
		var month = rDate.getMonth() + 1;  
		var date = rDate.getDate();  
		return year + "-" + dateHandle.add0(month) + "-" + dateHandle.add0(date); 
	},
	/**
	 * 比较第一个日期是否大于第二个
	 * @param {Object} date1
	 * @param {Object} date2
	 */
	compare: function(date1, date2) {
		var oDate1 = dateHandle.createDate(date1);
	 	var oDate2 = dateHandle.createDate(date2);
		return oDate1.getTime() > oDate2.getTime();
	},
	createDate: function(str) {
		var dateTemp = str.split("-");
		return new Date(dateTemp[0], Number(dateTemp[1]) - 1, dateTemp[2]);
	},
	add0: function(num) {
		if (Number(num) < 10) num = "0" + num; 
		return num;
	}
}
