From 054ef8fd1771a924bc1c854f9f47999f055c4e2d Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Tue, 29 Apr 2025 00:51:31 +0530 Subject: [PATCH] Updated TimePicker to disable future times and allow only current or past selections --- src/components/common/TimePicker.jsx | 29 ++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/common/TimePicker.jsx b/src/components/common/TimePicker.jsx index d4ab9cd4..92f31716 100644 --- a/src/components/common/TimePicker.jsx +++ b/src/components/common/TimePicker.jsx @@ -27,16 +27,37 @@ const TimePicker = ({ label, onChange, interval = 10, value }) => { return `${hoursFormatted}:${minutesFormatted} ${period}`; }; + // all slots + // const generateTimeSlots = () => { + // const slots = []; + // let currentTime = new Date(getCurrentTime()); + // for (let i = 0; i < 144; i++) { // 24 hours with custom interval + // slots.push(formatTime(currentTime)); + // currentTime.setMinutes(currentTime.getMinutes() + interval); + // } + // return slots; + // }; + // imit slots to current time: const generateTimeSlots = () => { const slots = []; - let currentTime = new Date(getCurrentTime()); - for (let i = 0; i < 144; i++) { // 24 hours with custom interval - slots.push(formatTime(currentTime)); - currentTime.setMinutes(currentTime.getMinutes() + interval); + let currentTime = new Date(); + currentTime.setSeconds(0); + currentTime.setMilliseconds(0); + + const endTime = new Date(currentTime); // current time as upper limit + + let slotTime = new Date(); + slotTime.setHours(0, 0, 0, 0); // start from 00:00 + + while (slotTime <= endTime) { + slots.push(formatTime(new Date(slotTime))); + slotTime.setMinutes(slotTime.getMinutes() + interval); } + return slots; }; + const handleSelect = (selectedTime) => { setTime(selectedTime);