This custom Validator that I got from here is not working after migrating our project to .NET 5. It is not removing the time part of the date.
public class DateAttribute : RangeAttribute
{
public DateAttribute(int min, int max)
: base(typeof(DateTime), DateTime.Now.AddYears(min).ToShortDateString(), DateTime.Now.AddYears(max).Date.ToShortDateString()) { }
}
[Required, DisplayName("Date")]
[Date(-2, 0, ErrorMessage = "The Date should be within the last two years.")]
public DateTime Date { get; set; }
This is what the html input looks like upon inspecting it:
<input type="date" value="2000-05-07" data-val="true" data-val-range="The Date should be within the last two years." data-val-range-max="04/12/2021 00:00:00" data-val-range-min="04/12/2019 00:00:00" data-val-required="The Date field is required." id="Date" name="Date">
I have tried using ToString("yyyy-MM-dd"), ToString("d"), and hardcoding the min and max inside the AddYears() methods, but the same thing happens.
It was working fine before on .NET Core 3.1, which we were using until 4 days ago. That's the only thing I can think of that was changed.
Does anyone have any ideas?