I tried Matias' answer above and had a flew glitches. After a bit of trouble shooting and slightly modifying Matias' answer, I managed to create a working solution.
If you paste the below code into a blank .html file then you should see a input box with a date widget attached to it.
The start date (8th August 2021), end date (26th August 2021) and dates in between should have their own distinct colour scheme.
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js" integrity="sha256-VBLiveTKyUZMEzJd6z2mhfxIqz3ZATCuVMawPZGzIfA=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/js/bootstrap-datetimepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/css/bootstrap-datetimepicker.min.css" rel="stylesheet"></link>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style>
.StartDate{background-color: green;}
.InBetweenDates{background-color: darkred;}
.EndDate{background-color: blue;}
</style>
<p>Date: <input type="text" readonly="" id="elementPicker"></p>
<script>
var inRange=false;
$('#elementPicker').datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true,
minDate: new Date("01/01/2016"),
beforeShowDay: function(date){
console.log("date")
console.log(date)
dateFormat = date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
console.log("dateFormat")
console.log(dateFormat)
// 7 means August (0 means January)
// We are checking to see if the date is 8th August 2021
// Matias had if (dateFormat == '2014-01-01') but after looking at Console.Log,
// I removed the leading 0 from the day and month figure.
if (dateFormat == '2021-7-8') {
console.log("(dateFormat == '2021-7-8')")
inRange = true; //to highlight a range of dates
// If the day is 8th August 2021 (remember 7 means August not July)
// then we add the CSS class StartDate to the day (look at the top of the code for the StartDate CSS class styling).
return [true, 'StartDate', 'tooltipText'];
}
if (dateFormat == '2021-7-26') {
console.log("(dateFormat == '2021-7-26')")
inRange = false; //to stop the highlight of dates ranges
// If the day is 26th August 2021 (remember 7 means August not July)
// then we add the CSS class EndDate to the day (look at the top of the code for EndDate CSS class styling).
return [true, 'EndDate', 'tooltipText'];
}
if (inRange) {
// If the day is between 8th August 2021 - 26th August 2021 then we add the CSS class InBetweenDates to the day (look at the top of the code for the StartDate CSS class styling).
return [true, 'InBetweenDates', 'tooltipText'];
} else {
// I think this essentially means select the day, do nothing with it and then move on.
// Adding this in prevented the error message 'Uncaught TypeError: Cannot read property '0' of undefined jquery datepicker'.
return [true];
}
}
});
</script>