JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Date and Time Range Selector</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<style>
.date-time-range-selector {
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
}
.calendar-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.month-container {
margin: 10px;
}
.time-slider {
width: 200px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="date-time-range-selector">
<label for="dateTimeRange">Select Date and Time Range:</label>
<input type="text" id="dateTimeRange" readonly>
<div id="calendar-popup" style="display: none;">
<div class="calendar-container"></div>
</div>
</div>
</body>
</html>
CSS
.ui-datepicker .in-range a,
.ui-datepicker .range-start a,
.ui-datepicker .range-end a {
background-color: #FF0000;
color: #FFFFFF;
}
.ui-datepicker .range-start a,
.ui-datepicker .range-end a {
font-weight: bold;
}
.ui-datepicker-other-month {
visibility: visible;
}
.ui-datepicker-today {
background: none !important;
}
JavaScript
$(function() {
const $input = $("#dateTimeRange");
const $calendarPopup = $("#calendar-popup");
const $calendarContainer = $calendarPopup.find(".calendar-container");
let startDate, endDate, startTime, endTime;
function updateInputValue() {
if (startDate && endDate && startTime && endTime) {
$input.val(`${startDate.toLocaleDateString()} ${startTime} - ${endDate.toLocaleDateString()} ${endTime}`);
} else if (startDate && startTime) {
$input.val(`${startDate.toLocaleDateString()} ${startTime} - Select end date`);
} else {
$input.val('');
}
}
function createCalendar(date) {
const $monthContainer = $("<div>").addClass("month-container");
const $calendar = $("<div>").datepicker({
defaultDate: date,
numberOfMonths: [2,3],
showOtherMonths: true,
selectOtherMonths: true,
beforeShowDay: function(date) {
const isInRange = startDate && endDate && date >= startDate && date <= endDate;
const isStart = startDate && date.getTime() === startDate.getTime();
const isEnd = endDate && date.getTime() === endDate.getTime();
let cssClass = '';
if (isStart) cssClass = 'in-range';
if (isEnd) cssClass = 'in-range';
if (isInRange) cssClass = 'in-range';
return [true, cssClass];
},
onSelect: function(selectedDate, instance) {
const selected = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
if (!startDate || (startDate && endDate) || (startDate && selected < startDate)) {
startDate = selected;
endDate = null;
startTime = '00:00';
endTime = null;
} else {
endDate = selected;
...