As shown below in the HTML code, I have a date-picker. I want to set the max and min values of the date programmatically from the typescript code. I want to set the min and the max values of the date so that the user cannot select any date beyond the max
value set, and cannot select any date below the min value set.
The desired value for the [max]
should be today’s date.
I tried the following approach, but still, the user can select a date value greater than today’s date.
this.maxDateLimit = formatDate(new Date(), 'yyyy-MM-dd ', 'en')
Please let me know how to correct this.
html:
<div id="idDateOfSprayContainer">
<clr-date-container class="clsDateContainer">
<label id="idDateOfSprayLabel">
{{ "SITE.INSECTICIDES.DATE_OF_SPRAY_LABEL" | translate }}:
</label>
<input id="idDateOfSprayValue" class="clr-control-container" clrDate type="date" placeholder="" [(ngModel)]="iDatePasser.dateOfSpray" name="dateOfSpray" (ngModelChange)="onDateOfSpraySet($event)"
value="2021-07-21" [min]="minDateLimit" [max]="maxDateLimit" [disabled] = "!siteSelectionState"/>
</clr-date-container>
</div>
3
Answers
Always we use a library, we need take account the "Inputs" of the component and the type of this Inputs.
In the case of clarity date-picker you see the "Inputs" in "Summary of Options" of the docs (at the end of the document)
See that the "min" and "max" inputs are strings in the way "yyyy-MM-dd"
So checking the custom format option in Angular
NOTE: I use the local "en-US" because if the installed by defect in Angular -we needn’t add any local more-. In this case it’s indiferent use ‘en-US’ than, e.g. if we added locale-es for Spanish ‘es-ES’
Withour prior knowledge of angular, I was able to come up with this by reading the documentation and some trial and error:
Dates beyond today cannot be selected:
You can enter a later date by typing in, for example,
12/31/2050
, but you will then see an error message when you submit the form:Note that I did not put
max
in square brackets.You should use min & max like this for your case.
This is the angualr Date Pipe.For more info on date pipe you can visit angualr documentation link https://angular.io/api/common/DatePipe
Date pipe will convert to the value binded to our required format.
As other member pointed out that it should be in string format only.