0

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?

Rena
  • 23,238
  • 5
  • 24
  • 53
Trevor
  • 39
  • 8

2 Answers2

0

I believe it's a timezone issue. Is the time a consistent value when it shows up? For instance, when I ran into this issue the time always said 5AM because it converts from my timezone to UTC (or the other way around).

Luke Weaver
  • 275
  • 1
  • 9
  • Hi, time is always 00:00:00 in the data-range attribute values – Trevor Apr 14 '21 at 17:23
  • Sometimes the time zone issue occurs between the client and server. JS adds the time zone to the date and c# tries to convert it to UTC when converting from JS which changes the time. Is the time value present when you get Date in your controller? – Luke Weaver Apr 14 '21 at 20:57
0

It seems you want to display only date in the generated data-val-range-max and data-val-range-min, you need change your customed RangeAttribute like below:

public class DateAttribute : RangeAttribute, IClientModelValidator
{
    public int Min { get; set; }
    public int Max { get; set; }
    public DateAttribute(int min, int max)
      : base(typeof(DateTime), DateTime.Now.AddYears(min).ToShortDateString(), DateTime.Now.AddYears(max).Date.ToShortDateString()) 
    {
        Min = min;
        Max = max;
    }
    
    public void AddValidation(ClientModelValidationContext context)
    {
        context.Attributes.Add("data-val-range-max", DateTime.Now.AddYears(Max).ToShortDateString());
        context.Attributes.Add("data-val-range-min", DateTime.Now.AddYears(Min).Date.ToShortDateString());
    }
}
Rena
  • 23,238
  • 5
  • 24
  • 53
  • Is there a way to do it without having to write a jQuery script? It is working fine as it is, it's just that the time isn't removed so any value that is input automatically fails – Trevor Apr 14 '21 at 18:49
  • What do you mean? Actually in my code I do not write any jQuery script. – Rena Apr 15 '21 at 01:22
  • I am new, my apologies. What is the AddValidation method doing? It doesn't seem like it is being called... – Trevor Apr 15 '21 at 20:42
  • You could see that your html have many `data-xxx` elements.They are generated by this method. By default, it will generate the datetime, but you want to generate only date. So you could use this method to manually generate the html. If my answer help you resolve your issue, could you please accept as answer? Refer to:[How to accept as answer](https://meta.stackexchange.com/a/5235/673311).Thanks. – Rena Apr 16 '21 at 01:19
  • Hi @Trevor, Any update? Did my answer help you resolve your issue? – Rena Apr 30 '21 at 01:25