JavaScript计算两个日期时间段内日期的方法
本文实例讲述了JavaScript计算两个日期时间段内日期的方法。分享给大家供大家参考。具体实现方法如下:
/************************* *计算两个日期时间段内所有日期 * *@paramvalue1 * 开始日期YYYY-MM-DD *@paramvalue2 * 结束日期 *return日期数组 */ functiondataScope(value1,value2){ vargetDate=function(str){ vartempDate=newDate(); varlist=str.split("-"); tempDate.setFullYear(list[0]); tempDate.setMonth(list[1]-1); tempDate.setDate(list[2]); returntempDate; } vardate1=getDate(value1); vardate2=getDate(value2); if(date1>date2){ vartempDate=date1; date1=date2; date2=tempDate; } date1.setDate(date1.getDate()+1); vardateArr=[]; vari=0; while(!(date1.getFullYear()==date2.getFullYear() &&date1.getMonth()==date2.getMonth()&&date1.getDate()==date2 .getDate())){ vardayStr=date1.getDate().toString(); if(dayStr.length==1){ dayStr="0"+dayStr; } dateArr[i]=date1.getFullYear()+"-"+(date1.getMonth()+1)+"-" +dayStr; i++; /* *document.write("<divstyle='display:block'>"+date1.getFullYear()+ *"-"+(date1.getMonth()+1)+"-"+date1.getDate()+"</div>"); */ //document.write(dateArr[i]+"<br>"); date1.setDate(date1.getDate()+1); } returndateArr; }
希望本文所述对大家的javascript程序设计有所帮助。