0

I'm creating a custom ASP.Net validator that checks if the data entered is a non working day.

The definition of "non working day" is:

  1. The date is either a Saturday or Sunday
  2. The date is a national holiday

I've coded the c# stuff and have created expando attributes for the error message and a string of holiday dates. Having done that, I've created a javascript function that contains this code to check for none working days. I've removed other code for brevity.

if (sender.NonWorkingDayErrorMessage && sender.Holidays) {

    // Is weekend
    if (date.getDay() == 6 || date.getDay() == 0) {
        sender.innerHTML = sender.NonWorkingDayErrorMessage;
        return;
    }

    // Is holiday
    var holidays = sender.Holidays.split(";");

    for (var i = 0; i < holidays.length; i++) {

        var h = new Date(Date.parse(holidays[i]));

        if (h === date) {
            sender.innerHTML = sender.NonWorkingDayErrorMessage;
            return;
        }
    }
}

The issue I have is that the code h === date is always false - here's the output I get when I add an alert and type in 26/8/2013.

Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => false

As you can see I parse the holidays but I also test the input 'date' like this further up the function like this:

// Deconstruct string and reconstruct
// as date.
var parts = args.Value.split("/");
var day = parseInt(parts[0],10);
var month = parseInt(parts[1],10) -1;
var year = parseInt(parts[2],10);
var date = new Date(year, month, day);

// Valid date format but not a valid date
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
    sender.innerHTML = sender.InvalidErrorMessage;
    return;
}

Anyone got ideas as to why these two dates aren't seen as matches?

3
  • when dealing with dates comparisons and parsing, I'd highly recommend you to use an external library. This one works great: code.google.com/p/datejs Commented Jul 22, 2013 at 11:26
  • Another option would be the node-date-utils lib (usable in browser too). Commented Jul 22, 2013 at 11:49
  • If you already have it working properly server side just use AJAX for the validation. Extra requests, but muss less headache. Commented Jul 22, 2013 at 11:52

1 Answer 1

2

Try like this

var  a = new Date(2013,12,1);
var  b = new Date(2013,12,1);

a.getTime() === b.getTime() 
Sign up to request clarification or add additional context in comments.

1 Comment

Just needed to compare with getTime();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.