1

I'm trying to use the validator components but I'm having trouble with daylight saving time.

It all started with a simple markup on a clean new website project:

<form id="form1" runat="server">
<div>
  <asp:TextBox ID="TextBoxTest" runat="server"></asp:TextBox>
  <asp:RangeValidator ID="RangeValidatorTest" runat="server"
    Type="Date" MinimumValue="17/09/2013" MaximumValue="12/11/2014"
    ControlToValidate="TextBoxTest" ErrorMessage="Invalid Date">
  </asp:RangeValidator>
</div>
</form>

I should mention that all dates written on this question are "dd/MM/yyyy". I edited the web.config file and set the culture and uiculture to "pt-BR" and run the application.

Now, with these settings, if I try to input "20/10/2013" or "19/10/2014" it says it is not a valid date.

Those are exactly the dates when it will start the daylight saving time on those years.

If I turn off the option on my dev machine to change the date and time automatically the validator works properly.

Going further, I debugged the client javascript generated by the validators (the culture and uiculture were set on web.config - so I expect the script to be generated accordingly). The error happens in a function called ValidatorConvert. Here's the piece causing the problem:

var date = new Date(year, month, day);

return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;

On my machine, the constructor for the Date object on the first line with 2013, 10 and 20 as parameters create an object with the following: Sat Oct 19 23:00:00 UTC-0300 2013. This means it is respecting my Windows configuration and subtracting automatically 1 hour due to the start of daylight saving time. And because of that the day portion is different than the original one which makes the validation fail as if it were an invalid date.

Is that the way it is supposed to work? It seems like the Javascript for the validators aren't really dynamic as I thought.

How can I work with daylight saving time and those validators?

By the way, I did this test on Visual Studio 2010 (and all service packs available) and .Net Framework 4.0.

I know a couple ways to get out of this problem if I was using Asp.Net MVC but this example belongs to a much bigger system built with WebForms.

For the sake of clarity, here's two debugger screens:

http://img692.imageshack.us/img692/2268/son0.png

http://img14.imageshack.us/img14/4963/dhbl.png

Rodrigo Lira
  • 1,489
  • 3
  • 14
  • 28
  • What is your system time zone? – Michael Liu Sep 18 '13 at 02:58
  • It's also GMT/UTC-03, as the Date generated on client side javascript. Disabling the windows configuration mentioned everything works (as the system won't try to subtract an hour automatically). Thanks for helping. – Rodrigo Lira Sep 18 '13 at 03:35
  • I can reproduce the problem in the current versions of Firefox and Chrome, but *not* in IE 11 Preview. So I'm not sure if the bug is in the browser or in the ASP.NET validation script. – Michael Liu Sep 18 '13 at 03:56
  • Here's a workaround: Copy and paste the whole `ValidatorConvert` function into a ` – Michael Liu Sep 18 '13 at 04:05
  • Indeed that works. IE9 and IE10 also have this problem. I wonder if IE11 do not rely on Windows Settings to generate the date objects. They might end up changing that before releasing anyway. I believe this is a javascript engine problem. It should not rely on Windows settings in my opinion. Most people probably just move to CustomValidator but this is not possible for me because Validators are all spread amongst multiple pages. In any case, thanks for taking the time. I really appreciated it. – Rodrigo Lira Sep 18 '13 at 17:16

0 Answers0