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?

4

1 回答 1

2

像这样试试

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

a.getTime() === b.getTime() 
于 2013-07-22T11:51:06.637 回答