首页 > 代码库 > js 日期按年月日增加
js 日期按年月日增加
<script>function isleapyear(year){ if(parseInt(year)%4==0 && parseInt(year)%100!=0)return true; if(parseInt(year)%400==0)return true; return false;}function datedrift(date,difference){ var year=parseInt(date.getUTCFullYear()), mon=parseInt(date.getUTCMonth())+1, day=parseInt(date.getUTCDate()); sumdays=[31,59,90,120,151,181,212,243,273,304,334,365,31,60,91,121,152,182,213,244,274,305,335,366]; if(typeof difference!="string" || difference.length==0)return date; var units=difference.match(/([+-]\d{1,3}[myd])/ig); for(var i=0;i<units.length;i++) { if(units[i].charAt(0)==‘+‘) { var mark=units[i].substr(-1,1); var value=http://www.mamicode.com/parseInt(units[i].substr(1,units[i].length-2)); if(mark ==‘y‘) { if(day<=28) { year=year+value; } else if(day==29) { year=year+value; if(mon==2) { if(!isleapyear(year)) day=28; } } else { year=year+value; } } else if(mark == ‘m‘) { year+=Math.floor((value+mon)/12); mon=(value+mon)%12; if(day==29 && mon==2 && !isleapyear(year))day=28; else if(day>=30 && mon==2) { if(isleapyear(year))day=29; else day=28; } else if(day==31 && (mon==4 || mon==6 || mon==9 || mon==11))day=30; } else if(mark== ‘d‘) { year+=Math.floor((day+sumdays[(mon<3?(mon-1):(isleapyear(year)?(mon+11):(mon-1)))-1]+value)/365); if(isleapyear(year)) { var days=((day+sumdays[(mon<3?(mon-1):(isleapyear(year)?(mon+11):(mon-1)))-1]+value)%365); if(days>=0 && days<31){mon=1;day=days==0?1:days} else if(days>=31 && days<60){mon=2;day=days==31?1:(days-31)} else if(days>=60 && days<91){mon=3;day=days==60?1:(days-60)} else if(days>=91 && days<121){mon=4;day=days==91?1:(days-91)} else if(days>=121 && days<152){mon=5;day=days==121?1:(days-121)} else if(days>=152 && days<182){mon=6;day=days==152?1:(days-152)} else if(days>=182 && days<213){mon=7;day=days==182?1:(days-182)} else if(days>=213 && days<244){mon=8;day=days==213?1:(days-213)} else if(days>=244 && days<274){mon=9;day=days==244?1:(days-244)} else if(days>=274 && days<305){mon=10;day=days==274?1:(days-274)} else if(days>=305 && days<335){mon=11;day=days==305?1:(days-305)} else if(days>=335 && days<=366){mon=12;day=days==335?1:(days-335)} } else { var days=((day+sumdays[(mon<3?(mon-1):(isleapyear(year)?(mon+11):(mon-1)))-1]+value)%365); if(days>=0 && days<31){mon=1;day=days==0?1:days} else if(days>=31 && days<59){mon=2;day=days==31?1:(days-31)} else if(days>=59 && days<90){mon=3;day=days==59?1:(days-59)} else if(days>=90 && days<120){mon=4;day=days==90?1:(days-90)} else if(days>=120 && days<151){mon=5;day=days==120?1:(days-120)} else if(days>=151 && days<181){mon=6;day=days==151?1:(days-151)} else if(days>=181 && days<212){mon=7;day=days==181?1:(days-181)} else if(days>=212 && days<243){mon=8;day=days==212?1:(days-212)} else if(days>=243 && days<273){mon=9;day=days==243?1:(days-243)} else if(days>=273 && days<304){mon=10;day=days==273?1:(days-273)} else if(days>=304 && days<334){mon=11;day=days==304?1:(days-304)} else if(days>=334 && days<=365){mon=12;day=days==334?1:(days-334)} } } } else { } } alert([year,mon,day]);}var curdate=new Date();datedrift(curdate,"+6m +3y +130d");</script>
js 日期按年月日增加
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。