I’m working on a script to pull and parse opening hours from an API. I created an object with the next seven days using regular hours and then compare to our list of holiday hours.
If a holiday occurs on one of these seven days, the opening hours are overwritten with this data.
// get regular hours
var next7days = [];
var storeday = new Date(2012, 12-1, 5);
for (var i = 0; i < 7; i++) {
var dayofweek = (storeday.getDay() + 6) % 7;
next7days[i] = data.hours.regular_hours[dayofweek];
next7days[i].date = storeday;
storeday = new Date(storeday.getTime() + 86400000);
}
// get holiday hours
for (var i = 0; i < data.hours.holiday_hours.length; i++) {
// this spits out all holidays fine
console.log('holiday:');
console.log(data.hours.holiday_hours[i]);
for (var j = 0; j < 7; j++) {
var fixdate = next7days[j].date.getFullYear() + '-' + (next7days[j].date.getMonth() + 1) + '-' + (next7days[j].date.getDate());
if (fixdate == data.hours.holiday_hours[i].date) {
next7days[j].open_time = data.hours.holiday_hours[i].open_time;
next7days[j].close_time = data.hours.holiday_hours[i].close_time;
next7days[j].day_name = data.hours.holiday_hours[i].day_name;
next7days[j].type = data.hours.holiday_hours[i].type;
// the first seven holidays are NOT successful
console.log('successful:');
console.log(next7days[j]);
}
}
}
This works brilliantly, though not for the first seven holiday dates. The script pulls in the info fine (see the console.log), but it either isn’t run through the holiday-regular comparison or it doesn’t recognize that the dates match.
- Here’s my fiddle: http://jsfiddle.net/chicgeek/3SKYx/48/
- And here’s the API source for reference: http://www.mec.ca/api/v1/stores/1
For reference, the first holiday dates are Dec 3 – Dec 9, but these seem to be ignored by my holiday hour comparison. Everything seems to work fine from Dec 10 and onwards.
Can anyone see why? Thanks in advance
The first few dates don’t work because they are missing the leading 0 that your holiday dates have. Then in double digits it works because the date equality works.