JavaScript日期比较应用小结

JavaScript日期比较在平时的工作中用到的很频繁,在这里做一下小小的总结,有利于自己积累,也有利于交流。

/*
 * @discription: 日期计算工具
 * @requires:
 */

	CalendarUtil = {

	/*
	 * @discription: 获取当前日期的日期串.
	 * @return:
	 *	{String} (YYYY-MM-DD)日期字符串.
	 */
	getDate: function() {
		var date = new Date();
		var month = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var da = date.getDate() <= 9 ? "0" + (date.getDate()) : (date.getDate());
		return (date.getFullYear() + "-" + month + "-" + da);
	},
	
	/*
	 * @discription: 返回几天之后的日期串.
	 * @params:
	 *  date - {String} (YYYY/MM/DD)日期字符串.
	 *  i - {int} 天数.
	 * @return:
	 *	{String} (YYYY/MM/DD)日期字符串.
	 */
	getDateAfter: function(date, i) {
		var r = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		if (r != null) {
			var date = new Date(r[1], r[3]-1, r[4]);
			date.setDate(date.getDate() + i);
			var month = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
			var da = date.getDate() <= 9 ? "0" + (date.getDate()) : (date.getDate());
			return date.getFullYear() + "-" + month + "-" + da;
		} else {
			return date;
		}
	},

	/*
	 * @discription: 返回几天之前的日期串.
	 * @params:
	 *  date - {String} (YYYY/MM/DD)日期字符串.
	 *  i - {int} 天数.
	 * @return:
	 *	{String} (YYYY/MM/DD)日期字符串.
	 */
	getDateBefore: function(date, i) {
		var r = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		if (r != null) {
			var date = new Date(r[1], r[3]-1, r[4]);
		date.setDate(date.getDate() - i);
		var month = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var da = date.getDate() <=9 ? "0" + (date.getDate()) : (date.getDate());
			return date.getFullYear() + "-" + month + "-" + da;
		} else {
			return date;
		}
	},

	/*
	 * @discription: 计算两个日期的天数差.
	 * @params:
	 *  date1 - {String} (YYYY/MM/DD)日期字符串.
	 *  date2 - {String} (YYYY/MM/DD)日期字符串.
	 * @return:
	 *	{string}  天数差.
	 */
	getDateDiff: function(date1, date2) {
		var aDate, oDate1, oDate2, iDays;
		var r1 = date1.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		oDate1 = new Date(r1[1], r1[3] - 1, r1[4]);

		var r2 = date2.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		oDate2 = new Date(r2[1], r2[3] - 1, r2[4]);
		iDays = parseInt((oDate1 - oDate2) / 1000 / 60 / 60 / 24);
		return iDays;
	},

	/*
	 * @discription: 计算与当前年份差.
	 * @params:
	 *  date - {String} (YYYY/MM/DD)日期字符串.
	 * @return:
	 *	{int}  与当前年份差.
	 */
	getYearDiff: function(oldDate, newDate) {
		var r1 = oldDate.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		var oldYear = r1[1];
		var newYear = new Date().getFullYear();
		if (typeof newDate != "undefined") {
			var r2 = newDate.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
			var newYear = r2[1];
		}
		return newYear - oldYear;
	},

	/*
	 * @discription: 获取时期data的星期数(字符串).
	 * @params:
	 *  date - {String} (YYYY/MM/DD)日期字符串.
	 * @return:
	 *	{string}  星期数(字符串).
	 */
	getStrWeek: function(date) {
		var r1 = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		var day = new Date(r1[1], r1[3] - 1, r1[4]).getDay();
		var week = "";
		switch(day) {
			case 0:
				week = "星期日";
				break;
			case 1:
				week = "星期一";
				break;
			case 2:
				week = "星期二";
				break;
			case 3:
				week = "星期三";
				break;
			case 4:
				week = "星期四";
				break;
			case 5:
				week = "星期五";
				break;
			case 6:
				week = "星期六";
				break;
		}
		return week;
	},
	
	/*
	 * @discription: 格式化时间,将yyyy-mm-dd格式成 yyyymmdd
	 * @params:
	 *  date - {String} (YYYY-MM-DD)日期字符串.
	 * @return:
	 *	{int}  yyyymmdd格式日期.
	 */
	formatSJ: function(sj){
     var s=sj.substring(0,4);
     var m=sj.substring(5,7);
     var n=sj.substring(8,10);
     //l+k+p;
     var InsertSJ=s+m+n;
	   return InsertSJ;
    },
   /*
	 * @discription: 判断时间逻辑关系,
	 * @params:
	 *  date1 - {String} (YYYY-MM-DD),date2 -{String} (YYYY-MM-DD)
	 * @return:
	 *	true 逻辑正确,false 逻辑不正确
	 */
	checkStartEnd: function(date1,date2){
	  var s=date1.substring(0,4);
    var m=date1.substring(5,7);
    var n=date1.substring(8,10);
    var startSJ=s+m+n;
    var y=date2.substring(0,4);
    var mon=date2.substring(5,7);
    var da=date2.substring(8,10);
    var endSJ=y+mon+da;
		if(startSJ <= endSJ){
			return true;
		}else{
			return false;
		}
	},
	 /*
	 * @discription: 得到当前时间并格式化成YYYY-MM-DD,
	 * @params:
	 *  
	 * @return:当前日期的格式化字符串 YYYY-MM-DD
	 *	
	 */
 getNowFormatDate: function(){
   var day = new Date();
   var Year = 0;
   var Month = 0;
   var Day = 0;
   var CurrentDate = "";
   //初始化时间
   Year= day.getFullYear();//ie火狐下都可以
   Month= day.getMonth()+1;
   Day = day.getDate(); 
   CurrentDate += Year + "-"; 
   if (Month >= 10 )
   {
    CurrentDate += Month + "-";
   }
   else
   {
    CurrentDate += "0" + Month + "-";
   }
   if (Day >= 10 )
   {
    CurrentDate += Day ;
   }
   else
   {
    CurrentDate += "0" + Day ;
   }
   return CurrentDate;
}
};

 

你可能感兴趣的:(JavaScript,IE)