日期:2014-05-16 浏览次数:20374 次
//格式化日期为年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