[javascript] 수령 시간 내 월요일 (*) 에 속 하 는 날짜 들

수요: 시작 시간 종료 시간 을 알 고 그 동안 월요일 이나 화요일 을 입력 하 십시오.
/*            (*)    
 * 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;
}

좋은 웹페이지 즐겨찾기