There is one question already :-
How can I disable past dates in angular p-calendar?
But I want to disable (or prevent) the past time also. For example if right now it is 14:00 Hrs, the user should not be able to enter anything less than that. Not even 13:59 Hrs. For preventing past date they have something called minDate
which I'm already using but there is nothing to restrict time.
I tried this first:
HTML:
<p-calendar (onInput)="onInput($event)" [minDate]="minDate" [showTime]="true" hourFormat="24">
</p-calendar>
TS:
onInput(e) {
console.log('Event: ', e);
}
But this way I wasn't able to capture the field value:
So I tried another approach inspired from these two questions:
How to get input value from in primeNG?
Compare two dates with JavaScript
HTML:
<p-calendar #myCalender (onInput)="onInput($event,myCalender)" ...>
</p-calendar>
TS:
onInput(e, mc) {
console.log('Value: ', mc.inputFieldValue);
const enteredDate = mc.inputFieldValue;
if (enteredDate.getTime() > new Date().getTime()) {
console.log('previous date');
// disable Apply button and apply red border css
} else {
console.log('future date');
// enable Apply button and remove red border css
}
}
This time I was able to get the value while every time I type into it but I still need help with comparison of dates logic. My logic is not working correctly. Please pitch in.