当前位置: 代码迷 >> JavaScript >> JS添任意时间
  详细解决方案

JS添任意时间

热度:271   发布时间:2012-09-22 21:54:54.0
JS加任意时间

//格式化日期为年4位,月日时分秒为2位的格式
	function formateDate(year,month,day,hour,minute,second){
		if(month<9)month="0"+month;
		if(day<9) day="0"+day;
		if(hour<9)hour="0"+hour;
		if(minute<9)minute="0"+minute;
		if(second<9)second="0"+second;
		return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}
	
	//函数调用说明
	//date 格式为2011-08-09 19:21:00,每位数字两个字符,如果是单数,则必须前面加0;
	//field 取值second  minute  hour  day  month  year 
	//n 取值second(-60~60) minute(-60~60) hour(-24~24) day(-31~30) month(-12~12) year(无穷小~无穷大)以上取值区间均不含头尾值。
	function showdate(date,field,n){//date为基数时间格式2011-08-09 19:21:00,n为秒,可正数也可负数
		var date = date.split(' ');
		var time = date[1].split(':');//时间19:21:00
		var date = date[0].split('-');//日期2011-08-09

		var v_second = parseFloat(time[2]);
		var v_minute = parseFloat(time[1]);
		var v_hour = parseFloat(time[0]);
		var v_day = parseFloat(date[2]);
		var v_month = parseFloat(date[1]);
		var v_year = parseFloat(date[0]);
		if(field=='second'){
			if(n>=60)return '';
			if(n<-60)return '';
			v_second = v_second+n;
			if(v_second>=0&&v_second<60){ //  0=<x<60
				return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
			}else if(v_second>-60&&v_second<0){// -60<x<0
				v_second = 60+v_second;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'minute',-1);
			}else{//因为 控制了n只能在-60到60之间取值(不含首尾),所以上面两个判断之后只剩下了 60=<x<120的可能了
				v_second = v_second-60;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'minute',1);
			}
		}else if(field=='minute'){
			if(n>60)return '';
			if(n<-60)return '';
			v_minute = v_minute+n;
			if(v_minute<60&&v_minute>=0){// 0=<x<60
				return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
			}else if(v_minute<0&&v_minute>-60){//  -60<x<0
				v_minute = v_minute+60;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'hour',-1);
			}else{// 60=<x<120
				v_minute = v_minute-60;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'hour',1);
			}
		}else if(field=='hour'){
			if(n>24)return '';
			if(n<-24)return '';
			v_hour = v_hour+n;
			if(v_hour>=0&&v_hour<24){// 0=<x<24
 				return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
			}else if(v_hour>-24&&v_hour<0){// <-24x<0
				v_hour = v_hour+24;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'day',-1);
			}else{// 24=<x<48
				v_hour = v_hour -24;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'day',1);
			}
		}else if(field=='day'){
			if(n>31) return '';
			if(n<-31)return '' ;
			v_day = v_day+n;
			var flag = false;//平润年标致 true表示是闰年
			if(v_year%4==0&&v_year%100!=0||v_year%400==0)flag=true;
			if(v_month==2){
				if(flag){//2月闰年
					if(v_day<=29&&v_day>0){ //0<x<=29&&闰年&&2月份
						return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
					}else if(v_day>-29&&v_day<=0){// -29<x<=0&&闰年&&2月份
						v_day = v_day + 31;//本月是二月份,向前一个月份借一个月,借的是一月份,一月份有31天
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',-1);
					}else{// 29<x<(29+31)&&闰年&&2月份
						v_day=v_day-29;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',1);
					}
				}else{//2月非闰年
					if(v_day<=28&&v_day>0){//0=<x<=28&&平年&&2月
						return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
					}else if(v_day>-28&&v_day<=0){//-28<x<=0&&平年&&2月
						v_day=v_day+31;//本月是二月份,向前一个月份借一个月,借的是一月份,一月份有31天
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',-1);
					}else{//28<x<(28+31)&&平年&&2月
						v_day=v_day-28;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',1);
					}
				}
			}else if(v_month==1||v_month==3||v_month==5||v_month==7||v_month==8||v_month==10||v_month==12){//1 3 5 7 8 10 12月份都有31天
				if(v_day<=31&&v_day>0){// 0<x<=31&& x in (1,3,5,7,8,10,12)
					return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				}else if(v_day<=0&&v_day>-31){// -31<x<=0&& x in (1,3,5,7,8,10,12)
					var marchFlag = false;
					if(v_month==3&&flag){//当前月份是润年的3月,那向上一个月就只能借到29天
						v_day=v_day+29;
						marchFlag = true;
					}else if(v_month==3&&!flag){//当前月份是平年的3月,那向上一个月就只能借到28天
						v_day=v_day+28;
						marchFlag = true;
					}
					
					if(marchFlag == true && v_day<=0){//如果借了2月的天数之后 还是小于0 ,那么要继续向上一个月,也就是一月借31天
						v_day=v_day+31;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',-2);//借了2月份的 还要借1月份的
					}else if(v_day<=0){
						v_day=v_day+30;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',-1);
					}else{
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',-1);
					}
				}else{// 31<x<(31+31)&& x in (1,3,5,7,8,10,12)
					v_day=v_day-31;
					if(v_day>28&&!flag&&v_month==1){//当前是平年的1月份,减去了31天后,还大于28天,二月份只有28天,也装不下,所以还要加一个月
						v_day=v_day-28;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',2);
					}else if(v_day>29&&flag&&v_month==1){//当前是润年的1月份,减去了31天后,还大于29天,二月份只有29天,也装不下,所以还要加一个月
						v_day=v_day-29;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',2);
					}else if(v_day>30&&v_month!=7){//减了31天后还大于30天,并且当前月份不是7月,那么还要加一个月。(因为非7月份的下一个月最多只有30天。)
						v_day = v_day-30;
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',2);
					}else{
						var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
						return showdate(temp,'month',1);
					}
				}
			}else{//非1 3 5 7 8 10 12月 非2月份 只有30天
				if(v_day<=30&&v_day>0){// 0<x<=30 && x in(4,6,9,11)
					return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				}else if(v_day>-31&&v_day<=0){//  (1-31)<x<=0 && x in(4,6,9,11)
					v_day=v_day+31;//当前月份的上一个月 无论如何都会是31天
					var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
					return showdate(temp,'month',-1);
				}else{// 30<x<(30+31)
					v_day=v_day-30;
					var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
					return showdate(temp,'month',1);
				}
			}
		}else if(field=='month'){
			if(n>12)return '';
			if(n<-12)return '';
			v_month= v_month+n;
			if(v_month<=12&& v_month>0){// 0<x<=12
				return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
			}else if(v_month>-12&&v_month<=0){//  -12<x<=0
				v_month=v_month+12;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'year',-1);
			}else{//  12<x<24
				v_month=v_month-12;
				var temp = formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
				return showdate(temp,'year',1);
			}
		}else if(field=='year'){
			v_year =v_year+n;
			//以下代码处理类似 2010-02-29 12:12:12加一年后变成2011-02-29 12:12:12的情况,2011年并不是闰年,所以没有29号
			var flag = false;//平润年标致 true表示是闰年
			if(v_year%4==0&&v_year%100!=0||v_year%400==0)flag=true;
			if(v_month==2&&v_day==28&&flag)v_day=29;
			
			
			return formateDate(v_year,v_month,v_day,v_hour,v_minute,v_second);
		}
	}
	
	
	function date1MinusDate2(date1,date2){
		var date1 = date1.split(' ');
		var time1 = date1[1].split(':');//时间19:21:00
		var date1 = date1[0].split('-');//日期2011-08-09
		var second1 = parseFloat(time1[2]);
		var minute1 = parseFloat(time1[1]);
		var hour1 = parseFloat(time1[0]);
		var day1 = parseFloat(date1[2]);
		var month1 = parseFloat(date1[1])-1;
		var year1 = parseFloat(date1[0]);
		debugger;
		var date2 = date2.split(' ');
		var time2 = date2[1].split(':');//时间19:21:00
		var date2 = date2[0].split('-');//日期2011-08-09
		var second2 = parseFloat(time2[2]);
		var minute2 = parseFloat(time2[1]);
		var hour2 = parseFloat(time2[0]);
		var day2 = parseFloat(date2[2]);
		var month2 = parseFloat(date2[1])-1;
		var year2 = parseFloat(date2[0]);
		
		var d1 = new Date(year1,month1,day1,hour1,minute1,second1);		
		var d2 = new Date(year2,month2,day2,hour2,minute2,second2);	
		
		var result = (d1.getTime()-d2.getTime())/(24*60*60*1000)
		return result;
	}
?
  相关解决方案