<input type="date" onChange={(e)=>setDate(e.target.value)}/>
<input type="time" onChange={(e)=>setTime(e.target.value)} />
if the date is after today and time is after current time i want to show validation that "please select precious time" .
I want the validation on time and date.
2
Answers
You can achieve this by creating a new Date object with the current date and time, and then comparing it with the selected date and time.
So your react component should look like the below:
To achieve the mentioned, I would take this approach:
Let’s go through the code above:
handleDateChange
updates the date state whilehandleTimeChange
updates the time state, then callsvalidateDateTime
to check the selected date and the time. Then,validateDateTime
function constructs a Date object from the selected date and time strings, compares the selected date and time to the current one. It checks if the selected date and time are both in the future.