今天碰到一个需求,需要根据时间搓()的开始时间和结束时间计算出具体礼拜几然后显示在table里的表头上,于是写一个下面的timeCount方法,传入开始时间和结束即可,比如 timeCout(“-10-1”,“-10-7”)即可调用;
function getDate(datestr){var temp = datestr.split("-");var date = new Date(temp[0],temp[1],temp[2]);return date;}//根据开始时间和结束时间计算具体礼拜几export function timeCount(start,end){var startTime = getDate(start);var endTime = getDate(end);let newArr=[]while((endTime.getTime()-startTime.getTime())>=0){var year = startTime.getFullYear();var month = startTime.getMonth().toString().length==1?"0"+startTime.getMonth().toString():startTime.getMonth();var day = startTime.getDate().toString().length==1?"0"+startTime.getDate():startTime.getDate();let newDay=year+"-"+month+"-"+day;newArr.push(newDay)console.log(year+"-"+month+"-"+day);startTime.setDate(startTime.getDate()+1);}let finnalArr=newArr.map(item=>{switch(new Date(item).getDay()){case 1:return {title:"星期一"};break;case 2:return {title:"星期二"};break;case 3:return {title:"星期三"};break;case 4:return {title:"星期四"};break;case 5:return {title:"星期五"};break;case 6:return {title:"星期六"};break;case 0:return {title:"星期天"};break;}})return finnalArr; }