I'm working on an order form for my website. For the orders the requirement is 4 days clear notice(i.e. Monday for Friday, Tuesday for Saturday etc), but for orders made on a Saturday and Sunday the minimum day they can get is the following Thursday. They are also not allowed to select Sundays as a day for delivery.
This is my date field:
<p>What date is this required for?</p>
<p>Please note, we need 4 days clear notice(Monday-Friday)</p>
<input id="datefield" name="date" type='date' onkeydown="return false" min='2019-05-10' max='2000-13-13'></input>
This is the js I have so far for not letting them pick sundays and for making the earliest date selection 4 days in advance
<script>
var date = document.querySelector('[type=date]');
function noSundays(e){
var day = new Date( e.target.value ).getUTCDay();
// Days in JS range from 0-6 where 0 is Sunday and 6 is Saturday
if( day == 0 ){
e.target.setCustomValidity('Unfortunately we cannot deliver cakes on a Sunday, please select another day');
} else {
e.target.setCustomValidity('');
}
}
date.addEventListener('input',noSundays);
</script>
<script>
var today = new Date();
var dd = today.getDate()+4;
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
today = yyyy+'-'+mm+'-'+dd;
document.getElementById("datefield").setAttribute("min", today);
</script>
So this is fine and works but how would I go about making a condition if the day they are ordering is a Saturday or Sunday that the minimum date they can select is the following Thursday?
Managed to get it working by doing the following:
var today = new Date(); // current date/time
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
if (dd == "03" || dd =="04" || dd=="5" || dd=="06") {
dd = today.getDate()+5;
if(dd<10){
dd='0'+dd
}
today = yyyy+'-'+mm+'-'+dd;
document.getElementById("datefield").setAttribute("min", today);
}
else {
dd = today.getDate()+4;
if(dd<10){
dd='0'+dd
}
today = yyyy+'-'+mm+'-'+dd;
document.getElementById("datefield").setAttribute("min", today);
}
Might not be the best way but it works