Not sure if you want jQuery UI datepicker. Here is how to do this using the plugin. You can use maxDate
and minDate
to get your range, and beforeShowDay
with datepicker.noWeekends
to disable all weekends. minDate
set to 0
will disable all days before today.
EDIT: added a better option that will not change with the current time for maxDate
.
Then set a variable up for date, month and year, get each using a new Date()
obj:
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
Then you can set your maxDate
to new Date(year, month+1, '31')
.
$(document).ready(function() {
$(function() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
$("#cal").datepicker({
beforeShowDay: $.datepicker.noWeekends,
maxDate: new Date(year, month + 1, '31'),
minDate: 0,
});
});
})
.ui-datepicker {
background-color: #fff;
}
.ui-datepicker-header {
background-color: #616eff;
font-family: 'Teko', sans-serif;
font-size: 2rem;
}
.ui-datepicker-title {
color: #1d1d1d;
}
.ui-widget-content .ui-state-default {
font-family: 'Teko', sans-serif;
font-size: 1.5rem;
border: 0;
text-align: center;
background: #fff;
font-weight: normal;
color: #000;
}
.ui-widget-content .ui-state-default:hover {
border: 0;
text-align: center;
background: #000;
font-weight: normal;
color: #eee;
box-shadow: 0px 0px 4px #000;
transform: scale(1.1);
transition: transform .15s;
}
.ui-widget-content .ui-state-active {
border: 0;
background: #616eff;
color: #fff;
box-shadow: 0px 0px 4px #000;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">
</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Teko:wght@300&display=swap" rel="stylesheet"> Date: <input id="cal" type="text" name="APPOINTMENTDATE" data-field-type="Text">
For a native datepicker with HTML 5 you can create an array of months and the use const date = new Date; const now = date.now()
, then you can format the string to match that which the min
attribute will take => yyyy-mm-dd
. Then use JS to place the string into your min attribute.
Then to handle the weekends, use your current code, however, instead of a pesky alert, create a span tag and append it to the parent element and place it next to your input with a warning and styling indicative to a warning. Use a timeout to remove the tooltip once the animation fades.
const months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
const now = new Date()
const formattedDate = now.getFullYear() + '-' + months[now.getMonth()] + '-' + now.getDate()
document.getElementById('date').min = formattedDate
document.getElementById('date').addEventListener('input', function(e) {
var day = new Date(this.value).getUTCDay();
if ([6, 0].includes(day)) {
e.preventDefault();
this.value = '';
const tooltip = document.createElement('SPAN')
tooltip.classList.add('tooltip')
tooltip.textContent = "Sorry weekends are not available!"
e.target.parentNode.append(tooltip)
setTimeout(() => {
e.target.parentNode.querySelector('.tooltip').remove()
},3000)
}
})
.warning {
background-color: pink;
color: darkred;
}
.parent {
position: relative;
}
.tooltip {
background-color: pink;
position: absolute;
left: 130px;
padding: .2rem .5rem;
border-radius: .3rem;
animation: fade 3s ease-in-out;
opacity: 0;
border: 1px solid darkred;
}
.tooptip::before {
content: '';
width: 10px;
height: 10px;
left: 20px;
top: 10px;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right:10px solid pink;
position: absolute;
}
@keyframes fade {
0%{
opacity: 1;
}
100% {
opacity: 0;
}
}
<div class="parent">
<input id="date" type="date" min="" max="2021-07-31" name="APPOINTMENTDATE" data-field-type="Text">
</div>