I am trying to learn more about HTML5 dates. I have added the following code to an MVC Core 2.1 view:
<label asp-for="DOB" class="control-label"></label>
@{
var minDate = DateTime.Now.AddYears(-99).Date;
var maxDate = DateTime.Now.AddYears(-18).Date;
var defaultDate = DateTime.Now.AddYears(-25).Date;
}
<input asp-for="DOB" class="datefield" type="date" min=@minDate max=@maxDate value=@defaultDate/>
<span asp-validation-for="DOB" class="text-danger"></span>
The HTML rendered looks like this:
<input class="datefield" type="date" min="23/10/1919 00:00:00" max="23/10/2000 00:00:00" value="23/10/1993 00:00:00" data-val="true" data-val-required="The DOB field is required." id="DOB" name="DOB" />
I have a few questions:
1) The default value of the date does not appear i.e. it says: dd/mm/yy. If I change my MVC code to this then it does work:
<input asp-for="DOB" class="datefield" type="date" min=@minDate max=@maxDate value="1900-01-01"/>
Why is it not accepting the variable specifically for the default value?
2) Are there any implications of changing the class to: datefield (from: form-control) in the MVC view.
3) Is it possible to specify a default date for the calendar, without the value appearing in the input field?