I hope this question will make sense. My javascript is fairly weak. However, I have a script used to pull information from our gyms software via apikey. Basically, at 7PM every night, if the workout has been posted I want the script to grab it. If it is prior to 7PM or after 7PM and tomorrow's workout isn't available, just keep previous days workout.
The script appears to work the majority of the time, however are running into issues when it is prior to noon. For instance, today, at 10:45AM, the script should have captured workout for 1/21/2017, but for some reason it was hitting the error code clause and keeping workout from 1/20/2017.
Sorry for wall of text, but basically was hoping someone may be able to tell me what/if there is a problem in my script. The area where I think the issue lies starts with the "today = new Date()"
Thank You
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
function loadFormattedWOD(selector, apiKey, date, location, program) {
$.ajax({
url: 'https://app.wodify.com/API/WODs_v1.aspx',
data: {
apiKey: apiKey,
date: date,
location: location,
program: program,
type: "json"
},
dataType: "json",
success: function(data){
try {
if(data && data.RecordList.APIWod.FormattedWOD) {
$(selector).html(data.RecordList.APIWod.FormattedWOD);
return "worked";
}
} catch(err) {
return null;
}
}
});
}
function loadFormattedWOD2(selector, apiKey, date, location, program) {
$.ajax({
url: 'https://app.wodify.com/API/WODs_v1.aspx',
data: {
apiKey: apiKey,
date: date,
location: location,
program: program,
type: "json"
},
dataType: "json",
success: function(data){
if(data && data.RecordList.APIWod.FormattedWOD) {
$(selector).html(data.RecordList.APIWod.FormattedWOD);
}
}
});
}
today = new Date()
if (today.getHours() >=19) {today.setDate(today.getDate()+1);}
else {today.setDate(today.getDate()+0);}
var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
if(loadFormattedWOD("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit") == null) {
retryWithToday();
}
function retryWithToday() {
today.setDate(today.getDate() - 1);
var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
loadFormattedWOD2("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit");
}
</script>
<div id="wodbody" />