js获取时间段内属于星期一(*)的日期们(代码教程)
程序员文章站
2022-06-17 10:12:34
需求:知道开始时间结束时间,求这段时间内输入星期一或星期二…的日期们
思路:用毫秒数来判断
/* 获取时间段内属于星期一(*)的日期们
* begin:...
需求:知道开始时间结束时间,求这段时间内输入星期一或星期二…的日期们
思路:用毫秒数来判断
/* 获取时间段内属于星期一(*)的日期们 * begin: 开始时间 * end:结束时间 * weekNum:星期几 {number} */ function getWeek (begin, end, weekNum){ var dateArr = new Array(); var stimeArr = begin.split("-");//=>["2018", "01", "01"] var etimeArr = end.split("-");//=>["2018", "01", "30"] var stoday = new Date(); stoday.setUTCFullYear(stimeArr[0], stimeArr[1]-1, stimeArr[2]); var etoday = new Date(); etoday.setUTCFullYear(etimeArr[0], etimeArr[1]-1, etimeArr[2]); var unixDb = stoday.getTime();//开始时间的毫秒数 var unixDe = etoday.getTime();//结束时间的毫秒数 for (var k = unixDb; k <= unixDe;) { let needJudgeDate = msToDate(parseInt(k)).withoutTime; //不加这个if判断直接push的话就是已知时间段内的所有日期 if (new Date(needJudgeDate).getDay() === weekNum) { dateArr.push(needJudgeDate); } k = k + 24*60*60*1000; } return dateArr; } //根据毫秒数获取日期 function msToDate (msec) { let datetime = new Date(msec); let year = datetime.getFullYear(); let month = datetime.getMonth(); let date = datetime.getDate(); let hour = datetime.getHours(); let minute = datetime.getMinutes(); let second = datetime.getSeconds(); let result1 = year + '-' + ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + '-' + ((date + 1) < 10 ? '0' + date : date) + ' ' + ((hour + 1) < 10 ? '0' + hour : hour) + ':' + ((minute + 1) < 10 ? '0' + minute : minute) + ':' + ((second + 1) < 10 ? '0' + second : second); let result2 = year + '-' + ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + '-' + ((date + 1) < 11 ? '0' + date : date); let result = { hasTime: result1, withoutTime: result2 }; return result; }