<script type="text/javascript"> getMonthBetween("2015-05-01","2016-05-01") function getMonthBetween(start,end){ var d1 = new Date(start); var d2 = new Date(end); var month1 = d1.getMonth() + 1,month2 = d2.getMonth() + 1; var year1 = d1.getFullYear() ,year2 = d2.getFullYear(); var minus; var monthSub = month2 - month1; var yearSub = year2 - year1; var ret; if(monthSub < 0){ minus = -1; if(yearSub <= 0){ alert('error'); return; } ret = (yearSub + minus) * 12 + (12 - month1) + month2; }else{ minus = 0; if(yearSub < 0){ alert('error'); return; }else{ ret = (yearSub + minus) * 12 + (month2 - month1); } } var msg = ''; var retArry = []; for(var i=0;i < ret+1;i++){ msg = year1 + "-" + ((month1 < 10) ? ("0" + month1) : month1); retArry.push(msg); if(month1 >= 12){ year1++; month1 = 1; } else month1++; } return( retArry); } </script>
为了计算两个时间之间相差几个月,起始月份要带入计算,由春哥帮忙,特收藏。