I am currently in the process of developing a custom ASP.Net validator that verifies whether the input data corresponds to a non-working day.
For the purpose of this validation, a "non-working day" is defined as follows:
- The date falls on either a Saturday or Sunday
- The date is considered a national holiday
I have written the necessary C# code and set up expando attributes for the error message and a list of holiday dates. In addition, I have created a JavaScript function containing the logic to identify non-working days. To streamline the example, I have omitted other sections of the code.
if (sender.NonWorkingDayErrorMessage && sender.Holidays) {
// Check if it's a weekend
if (date.getDay() == 6 || date.getDay() == 0) {
sender.innerHTML = sender.NonWorkingDayErrorMessage;
return;
}
// Check if it's a 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;
}
}
}
However, I am facing an issue where the comparison h === date always returns false. When I added an alert and inputted the date '26/8/2013', here is the output I received:
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
Even though I parsed the holidays correctly, the dates do not match. At the beginning of the function, I format the input 'date' like this:
// Split the string and reconstruct it as a 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);
// Validate the input date format
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
sender.innerHTML = sender.InvalidErrorMessage;
return;
}
If anyone has any insights into why these two dates are not recognized as matches, I would greatly appreciate your input.