Validation Time Travel

Posted by AgileCoder on April 8, 2009

On one of the primary web applications I maintain we have real-time help available to clients through an online chat application.  Usually when one of the chat line workers gets an interesting bug report they IM me and I try to fix the problem.

For weeks now I have occasionally been getting questions about our date validation. Specifically, we have people entering today's date and getting told that the date they have to enter must be today or earlier.  We have about 35 places where you can enter a date and the business requirement actually is that the date entered be 'today' or earlier.  We validate this on the server side, but also on the client using the JavaScript function show below. 

//Validate Past date
//---------------------------------------------//
function validatePastDate (control, sFieldName) 
{
    var c = document.getElementById(control);
    if (c != null ) 
    {
    //validate hasDate and isDate snipped//
        var d = new Date();
        var now = new Date();
        if(d.setTime(Date.parse(c.value, "n/j/y")) > now) 
        {
          sErr = sFieldName + " '" + c.value + "' must be a date earlier than today. \n"; 
        }
    }
    return sErr;    
}

Since the bug was opened in our issue database four different testers have tried to replicate it and none have been successful.  Yesterday I got pinged on IM and told that the 'funny date thing' was happening again.

I dropped everything and decided it was time to fix this once and for all.  I poured over the code, looking at the function and everywhere it was called.  No luck.  I ran it from my development box and from the beta server.  No dice.  I fired up an ancient desktop box that was kicking around and all of a sudden...ERROR!

Then I looked in the bottom right hand corner of the screen and noticed that it was 3:06 AM.  Bingo!  When I double-clicked the clock I discovered that the system date on that computer was 4/1/09, not 4/6/09.  It's clear now, but we all overlooked that

var now = new Date(); 

would report the client's system time to the client side validation...