var ShiftStrings = ["Shift 1 (23:00 - 07:00)", "Shift 2 (07:00 - 15:00)", "Shift 3 (15:00 - 23:00)"]; var ShiftTimes; var CurrentDate; var SelectedDate; var CurrentShift; var SelectedShift; function formatDate(date) { var d = new Date(date), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); } function SetCurrentDate() { var datetime = new Date(); if (datetime.getHours() == 23) { datetime.setDate(datetime.getDate() + 1); } CurrentDate = formatDate(datetime); if ($('#ShiftDate').length) { SelectedDate = $('#ShiftDate').val(); } else { SelectedDate = CurrentDate; } } function SetShiftTimes() { var datetime = new Date(); if (datetime.getHours() == 23 || datetime.getHours() < 7) { ShiftTimes = Times[0]; CurrentShift = 0; } else if (datetime.getHours() < 15) { CurrentShift = 1; } else { CurrentShift = 2; } if ($('#Shift').length) { SelectedShift = ShiftStrings.indexOf($('#Shift').val()); } else { SelectedShift = CurrentShift; } } SetShiftTimes(); SetCurrentDate(); $('#prev, #today, #next').on('click', function() { console.log("Shift Before:" + SelectedShift); if (this.id == "prev") { SelectedShift = SelectedShift - 1; if (SelectedShift < 0) { SelectedShift = 2; IncrementDate(-1); } } else if (this.id == "next") { SelectedShift = SelectedShift + 1; if (SelectedShift > 2) { SelectedShift = 0; IncrementDate(1); } } else { SelectedShift = CurrentShift; SelectedDate = CurrentDate; } document.getElementById('date').innerHTML = "Date: " + SelectedDate; document.getElementById('shift').innerHTML = "Shift: " + SelectedShift; function IncrementDate(i) { console.log("SelectedDate Before:" + SelectedDate); var tmpdate = new Date(SelectedDate); console.log("tmpDate Before:" + tmpdate); console.log("tmpDate Before:" + formatDate(tmpdate)); tmpdate.setDate(tmpdate.getDate() + i); console.log("tmpDate After:" + tmpdate); console.log("tmpDate After:" + formatDate(tmpdate)); SelectedDate = formatDate(tmpdate); console.log("SelectedDate After:" + SelectedDate); } }); document.getElementById('date').innerHTML = "Date: " + SelectedDate; document.getElementById('shift').innerHTML = "Shift: " + SelectedShift;