Page view counter

Curious behaviour when creating a Date() object using javascript

Last post 10-08-2007 10:08 AM by SrECosta. 6 replies.

Sort Posts:

  • Curious behaviour when creating a Date() object using javascript

    10-02-2007, 4:14 PM
    • Loading...
    • SrECosta
    • Joined on 10-02-2007, 4:11 PM
    • São Paulo, Brazil
    • Posts 4
    • Points 0

    Hi.

         I'm running ASP.NET 1.0, .NET Framework 1.1 and Internet Explorer 6.

         If I create a Date() object using javascript with the date "11/04/1973", the Date() object is created setting the day to day 03 instead of 04 as I expected.

         Take a look at my sample code, please.

    function DisplayDate()
    {
     // Original date: 11/04/1973
     var date = new Date(1973, 10, 4);
     // Displays "Sat Nov 3 23:00:00 UTC-0300 1973"
     alert(date);
     
     // If I change the date to: 11/05/1973
     var date = new Date(1973, 10, 5);   
     // Displays "Mon Nov 5 00:00:00 UTC-0200 1973"
     alert(date);
     
     // If I change the date to: 11/02/1973
     var date = new Date(1973, 10, 2);   
     // Display "Fri Nov 2 00:00:00 UTC-0300 1973"
     alert(date);
    }

         Could you explain that behaviour?

         Thanks in advance.

    A brazilian friend,
    Eduardo Costa.

    Eduardo Costa
    See my blog at blog.mutambal.com (portuguese)
  • Re: Curious behaviour when creating a Date() object using javascript

    10-02-2007, 7:07 PM
    • Loading...
    • bleroy
    • Joined on 04-12-2003, 7:09 AM
    • Redmond
    • Posts 2,295
    • Points 13,293
    • AspNetTeam

    Maybe that's a bug in an old version of IE because I can't reproduce that on IE7 or even IE6.0.2900.2180.xpsp_sp2_gdr.070227-2254.

    Bertrand
    ----
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: Curious behaviour when creating a Date() object using javascript

    10-03-2007, 8:50 AM
    • Loading...
    • SrECosta
    • Joined on 10-02-2007, 4:11 PM
    • São Paulo, Brazil
    • Posts 4
    • Points 0

    Hi. Thanks for the reply.

         I did the same test using IE 7(7.0.5730.11) and IE 6 (same version as you did). I start to think if that the problem is caused by some regional configuration (as I'm using portuguese language and settings here). I'll test it.

    Regards.

    Eduardo Costa
    See my blog at blog.mutambal.com (portuguese)
  • Re: Curious behaviour when creating a Date() object using javascript

    10-04-2007, 5:12 AM
    Answer

    SrECosta:
    var date = new Date(1973, 10, 4);
     // Displays "Sat Nov 3 23:00:00 UTC-0300 1973"
     

    Hi SrECosta,

    This behavior is by design. It relates to your time zone. From your description I understand that your time zone is “(GMT-03:00)Brasilia”. The daylight saving time for this time zone starts First Sunday in November at 00:00:00 and stops Last Sunday in February at 00:00:00. That’s why you get this result.

    The data of Brasilia’s daylight saving time is obtained from the following link:

    February 2007 cumulative time zone update for Microsoft Windows operating systems
    http://support.microsoft.com/kb/931836/en-us

     

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
  • Re: Curious behaviour when creating a Date() object using javascript

    10-04-2007, 10:41 AM
    • Loading...
    • SrECosta
    • Joined on 10-02-2007, 4:11 PM
    • São Paulo, Brazil
    • Posts 4
    • Points 0

    Hi Benson. Thanks for your reply!

         You know if is possible to create a Date() without considering the daylight saving settings?

         The main issue with this behaviour is that I'm working on an existing system that is using an asp.net rangevalidator to validate a birth day setting minimum date = 1/1/1900 and maximum date = today. If an user sets his birth day to 11/4/1973, the rangevalidator control fails, I mean, don't consider that date valid.

         Checking the asp.net javascript validation file "aspnet_client\system_web\1_1_4322\webuivalidation.js" I see that the function ValidatorConvert(op, dataType, val) fails just because of this behavior. In the line "return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;" it returns always null to that date. The "day == date.valueOf()" comparison fails.

         I don't think I'm allowed to change the webuivalidation.js file (and really I don't want to). Any suggestions?

          Thanks for all your support.

    Eduardo Costa.

    Eduardo Costa
    See my blog at blog.mutambal.com (portuguese)
  • Re: Curious behaviour when creating a Date() object using javascript

    10-07-2007, 10:07 PM
    Answer

    Hi SrECosta,

    Since you are using .NET framework 1.1, I think the simplest workaround is to modify the ValidatorConvert function. We can add hour, minute and second parameters for the Date constructor to fix this issue. The WebUIValidation.js file is under C:\Inetpub\wwwroot\aspnet_client\system_web\1_1_4322\
     
            var date = new Date(year, month, day,1,1,1);
            if (year < 100) {
                date.setFullYear(year);
            }
            return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;

    However, if you don’t want to modify the WebUIValidation.js file, I recommend you to use CustomValidator control instead. CustomValidator control has the ClientValidationFunction property for we can use our own JavaScript function to valuate the inputted date.

    For the JavaScript function of comparing dates, please check the following link:

    JavaScript Date Object
    http://www.w3schools.com/js/js_obj_date.asp

    For more information about the CustomValidator control, please refer to the following link:

    CustomValidator Class
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator(vs.71).aspx

     

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
  • Re: Curious behaviour when creating a Date() object using javascript

    10-08-2007, 10:08 AM
    • Loading...
    • SrECosta
    • Joined on 10-02-2007, 4:11 PM
    • São Paulo, Brazil
    • Posts 4
    • Points 0

    Ok.

         I'll change the javascript function. There are many pages that are using the rangevalidator. I can't change them one by one right now...

         Thanks for your support.

    Regards.

    Eduardo Costa.

    Eduardo Costa
    See my blog at blog.mutambal.com (portuguese)
Page 1 of 1 (7 items)