admin管理员组文章数量:1434949
In my angular application for date time selection I am using input type="datetime-local".
<input id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
required/>
Now I need to disable the dates that are previous to current date and the dates that are 3 days next to current date. For example for min, I have added validations as shown below. But the validations are not working, still the previous dates are getting enabled in the displayed calendar.
currentDate = new Date();
<input [min]="currentDate" id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
required/>
In my angular application for date time selection I am using input type="datetime-local".
<input id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
required/>
Now I need to disable the dates that are previous to current date and the dates that are 3 days next to current date. For example for min, I have added validations as shown below. But the validations are not working, still the previous dates are getting enabled in the displayed calendar.
currentDate = new Date();
<input [min]="currentDate" id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
required/>
Share
Improve this question
asked Jan 8, 2019 at 11:56
Madasu KMadasu K
1,8632 gold badges39 silver badges77 bronze badges
2
- Is the input to be used in any Angular forms? – Stol Commented Jan 8, 2019 at 12:12
- Yes, I am using angular template driven forms – Madasu K Commented Jan 8, 2019 at 12:18
3 Answers
Reset to default 2I would remend writing a custom validator for the form control. Min and max have bad browser support, this goes for datetime-local aswell though.
function dateValidator(c: FormControl) {
// Not sure if c will e in as a date or if we have to convert is somehow
const today = new Date();
if(c.value > today) {
return null;
} else {
return {dateValidator: {valid: false}};
}
}
...
myForm = this.formBuilder.group({
date: ['', dateValidator]
})
...
<input>
elements of type datetime-local
accepts values for min and max as string in yyyy-MM-ddThh:mm
format only. Since new Date()
returns a string which is not in the correct format min and max won't work. Just convert the date to the correct format like this.
currentDate = new Date().toISOString().substring(0, 16);
Here we are converting the date to the desired format first by converting it to a simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long and then removing the chars after yyyy-MM-ddThh:mm
Try to format your date with dashes:
Example:
pipe = new DatePipe('en-US');
minDate = new Date();
minDateOut = pipe.transform(minDate, 'yyyy-MM-dd');
maxDate = new Date(minDate.getTime() + (1000 * 60 * 60 * 24 * 3));
maxDateOut = pipe.transform(maxDate, 'yyyy-MM-dd');
<input
[min]="minDateOut"
[max]="maxDateOut"
id="field_bookingTime"
type="datetime-local"
class="form-control"
name="bookingTime"
[(ngModel)]="bookingTime"
required/>
Or just use any other date format without spaces...
本文标签: javascriptangular input type datetimelocal validationStack Overflow
版权声明:本文标题:javascript - angular input type datetime-local validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745625957a2666971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论