fix problems with offset

This commit is contained in:
Mark Eaton 2022-08-26 02:27:12 -04:00
parent 735e99a33b
commit d2eef24dbe
1 changed files with 23 additions and 26 deletions

View File

@ -7,6 +7,12 @@
$(document).ready(function(){
// a function to add the offset
Date.prototype.addHours= function(h){
this.setHours(this.getHours()+h);
return this;
}
$.ajax({
url: "https://kbcc-cuny.libcal.com/widget/hours/grid?iid=5570&format=json&weeks=1&systemTime=0",
type: 'GET',
@ -14,39 +20,30 @@ $.ajax({
dataType: 'jsonp'
})
.done( function (data) {
for (i = 0; i < data.locations.length; i++) {
for (let i = 0; i < data.locations.length; i++) {
if (data.locations[i].lid === 16733) {
for (let key in data.locations[i].weeks[0]) {
hours = data.locations[i].weeks[0][key]["rendered"];
weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
for (let j = 0; j < weekdays.length; j++) {
let x = weekdays[j];
hours = data.locations[i].weeks[0][x]["rendered"];
currently = data.locations[i].weeks[0][x]["times"]["currently_open"];
let start_and_end = hours.split(" - ");
let date = new Date(data.locations[i].weeks[0][key]["date"]);
let target_date = new Date(data.locations[i].weeks[0][x]["date"]);
offset = target_date.getTimezoneOffset() / 60;
target_date.addHours(offset);
let today = new Date()
const isToday = (today.toDateString() == date.toDateString());
let isToday = (today.toDateString() == target_date.toDateString());
if (isToday) {
if (start_and_end.length === 1) {
document.getElementById("hours-content").innerHTML += "The library is <strong>CLOSED</strong> today.<br />"
} else if (currently) {
document.getElementById("hours-content").innerHTML += "The library is currently <strong>OPEN</strong>.<br />Today's hours are <strong>" + hours + "</strong>.<br />";
} else {
let start = start_and_end[0];
let end = start_and_end[1];
let start_time = parseInt(start.replace(/\D/g, ""));
let start_ampm = start.slice(-2);
let end_time = parseInt(end.replace(/\D/g, ""));
let end_ampm = end.slice(-2);
let start_24 = start_time;
let end_24 = end_time;
if (start_ampm === "pm") { start_24 += 12};
if (end_ampm === "pm") { end_24 += 12};
console.log("system: " + today.getHours(), "start_24: " + start_24, "end_24: " + end_24);
let parsed_date = date.toLocaleDateString("en-US", { weekday: 'long', month: 'long', day: 'numeric' });
if (start_24 <= today.getHours() && today.getHours() < end_24) {
document.getElementById("hours-content").innerHTML += "The library is currently <strong>OPEN</strong>.<br />Today's hours are <strong>" + hours + "</strong>.<br />"
} else {
document.getElementById("hours-content").innerHTML += "The library is currently <strong>CLOSED</strong>.<br />Today's hours are <strong>" + hours + "</strong>.<br />"
}
document.getElementById("hours-content").innerHTML += "The library is currently <strong>CLOSED</strong>.<br />Today's hours are <strong>" + hours + "</strong>.<br />";
}
}
}
}
}
}
}
}
})
});
@ -56,7 +53,7 @@ $.ajax({
<style>
#hours-content-container {
margin-top: -10px;
margin-top: -10px;
text-align: center;
font-size: 1.7em;
}