'How to set minimum and max times in an ion-datetime
I am using an ionic DateTime component to pick just time in my page. But I have to just allow the user to select times from 06:30 Am to 07:30 PM. How can I limit the min-max times in ion-datetime
I have already tried giving min value to the component directly.
<ion-datetime displayFormat="hh:mm a" min="minDate"></ion-datetime>
Solution 1:[1]
you can specify them as props:
<ion-item>
<ion-label>Date</ion-label>
<ion-datetime displayFormat="MMMM YYYY" min="2016" max="2020-10-31" [(ngModel)]="myDate">
</ion-datetime>
</ion-item>
check the ion-datetime docs on Min and Max Datetimes
section
Solution 2:[2]
try with this :
I have using pickerFormate (24 Hr Formate) and displayValue (12 Hr Formate). it's similar your logic.
Homepage.html
<ion-item>
<ion-label>Time</ion-label>
<ion-datetime [hourValues]="hourValues" [min]="minTime" [max]="maxTime" displayFormat="hh:mm A" pickerFormat="HH:mm A" (ionFocus)="changeDate($event)"></ion-datetime>
</ion-item>
Homepage.ts
myDate: String = new Date().toISOString();
minTime = '06:30';
maxTime = '19:30';
hourValues = ['06','07','08','09','10','11','12','13','14','15','16','17','18','19'];
Result
Demo Link:
Solution 3:[3]
please try this
html code
<ion-datetime displayFormat="hh:mm a" [min]="minTime" [max]="maxTime"></ion-datetime>
ts code
minTime = '06:30';
maxTime = '07:30';
Solution 4:[4]
I tried this approach to get my bug cracked after crushing 6-7 days. In my case, I have to pass the getMinimum value as the minimum time while checking out. So users should not be able to check-out before the check-in time.
.ts File
check_in_value: 2021-12-06T13:15:11.684Z
const a = parseISO(check_in_value);
const b = addHours(new Date(a),5);
this.getMinimum = b.toISOString()
concole.log(this.getMinimum)
HTML File
<ion-datetime
displayFormat="YYYY-MM-DD HH:mm"
pickerFormat="YYYY-MM-DD HH:mm"
[value]="check_out"
[min]="getMinimum3"
></ion-datetime>
Important Notes:
The Displaying Format plays an important factor in rendering the Date and Time. Read the documentation thoroughly. “Ionic Framework uses the ISO 8601 datetime format for its value”. Link: ion-datetime: Ionic API Input for Datetime Format Picker
“Exactly the components shown here must be present, with exactly this punctuation” Link: https://ionicframework.com/docs/api/datetime?_gl=1*1lg7t6u*_ga*MTk3MzQ4NzM5Ny4xNjQxODg2ODQy*_ga_REH9TJF6KF*MTY1MDMyNDM1MS4xMTIuMS4xNjUwMzI2NjU0LjA.
“Exactly the components shown here must be present, with exactly this punctuation” Link: https://www.w3.org/TR/NOTE-datetime
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eslam Abu Hugair |
Solution 2 | |
Solution 3 | Jatin Devani |
Solution 4 | haris mahmood |