티스토리 뷰

🌈 JavaScript

날짜 범위 체크 로직

James Wetzel 2023. 5. 2. 16:46
728x90
반응형
const selectedDate = new Date($("#txt_new_warehouse_in_Date").val());
var currentDate = new Date($("#txt_warehouse_in_Date").val()); // current date
// 비교 연상자가 시간까지 계산식에 포함시기 때문에 시간을 0으로 설정
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0, 0, 0, 0); 

let pastDate = new Date(currentDate.getTime() - (5 * 24 * 60 * 60 * 1000));
let futureDate = new Date(currentDate.getTime() + (5 * 24 * 60 * 60 * 1000));

console.log("selectedDate : " + selectedDate);
console.log("currentDate : " + currentDate);
console.log("pastDate : " + pastDate);
console.log("futureDate : " + futureDate);

if (selectedDate >= pastDate && selectedDate <= futureDate) { } else {
    alert("New warehouse in date is outside 5 days range"); 
    return;
}

 

728x90
반응형