I work for a college, and we have specific start dates for each program. Most programs have a monthly start date while others skip a few months.
I want to write a code whereby we can update the start dates and cut off dates yearly, and it will display the next two upcoming start dates.
My idea is to define start dates and cut off dates, get the current date, create arrays for each “set” of start dates and each “set” of cut off dates, then compare the current date to the cut off dates and create new variables from the start date arrays with only the next two upcoming start dates in it.
// Set start dates for each month - Update yearly:
const janStartDate = "January 29";
const febStartDate = "February 26";
const marStartDate = "March 25";
const aprStartDate = "April 29";
const mayStartDate = "May 27";
const junStartDate = "June 24";
const julStartDate = "July 29";
const augStartDate = "August 26";
const sepStartDate = "September 30";
const octStartDate = "October 28";
const novStartDate = "November 25";
const decStartDate = "December 16";
// Set cut off dates - Update yearly:
const janCut = new Date(2024, 0, 19);
const febCut = new Date(2024, 1, 16);
const marCut = new Date(2024, 2, 15);
const aprCut = new Date(2024, 3, 19);
const mayCut = new Date(2024, 4, 17);
const junCut = new Date(2024, 5, 14);
const julCut = new Date(2024, 6, 19);
const augCut = new Date(2024, 7, 16);
const sepCut = new Date(2024, 8, 20);
const octCut = new Date(2024, 9, 18);
const novCut = new Date(2024, 10, 15);
const decCut = new Date(2024, 11, 13);
// Get current date:
const currentDate = new Date();
const currentMonth = currentDate.getMonth();
// Create arrays of cut off dates:
const monthlyCutOffDates = [janCut, febCut, marCut, aprCut, mayCut, junCut, julCut, augCut, sepCut, octCut, novCut, decCut]
const phrmCutOffDates = [febCut, marCut, mayCut, junCut, augCut, sepCut, novCut, decCut]
const techCutOffDates = [janCut, marCut, aprCut, junCut, julCut, sepCut, octCut, decCut]
const pswCutOffDates = [janCut, febCut, marCut, aprCut, mayCut, junCut, julCut, augCut, sepCut, novCut, decCut]
// Create arrays of start dates:
const monthlySet = [janStartDate, febStartDate, marStartDate, aprStartDate, mayStartDate, junStartDate, julStartDate, augStartDate, sepStartDate, octStartDate, novStartDate, decStartDate];
const phrmSet = [febStartDate, marStartDate, mayStartDate, junStartDate, augStartDate, sepStartDate, novStartDate, decStartDate];
const techSet = [janStartDate, marStartDate, aprStartDate, junStartDate, julStartDate, sepStartDate, octStartDate, decStartDate];
const pswSet = [janStartDate, febStartDate, marStartDate, aprStartDate, mayStartDate, junStartDate, julStartDate, augStartDate, sepStartDate, novStartDate, decStartDate];
// Limit the start date sets to only the two upcoming start dates:
let monthlyStartDatesToShow = [];
if (currentDate > monthlyCutOffDates[currentMonth]) {
monthlyStartDatesToShow.push(monthlySet[(currentMonth + 1)]);
monthlyStartDatesToShow.push(monthlySet[(currentMonth + 2)]);
}
else {
monthlyStartDatesToShow.push(monthlySet[currentMonth]);
monthlyStartDatesToShow.push(monthlySet[(currentMonth + 1)]);
}
let phrmStartDatesToShow = [];
if (currentDate > phrmCutOffDates[currentMonth]) {
phrmStartDatesToShow.push(phrmSet[(currentMonth + 1)]);
phrmStartDatesToShow.push(phrmSet[(currentMonth + 2)]);
}
else {
phrmStartDatesToShow.push(phrmSet[currentMonth]);
phrmStartDatesToShow.push(phrmSet[(currentMonth + 1)]);
}
let techStartDatesToShow = [];
if (currentDate > phrmCutOffDates[currentMonth]) {
techStartDatesToShow.push(techSet[(currentMonth + 1)]);
techStartDatesToShow.push(techSet[(currentMonth + 2)]);
}
else {
techStartDatesToShow.push(techSet[currentMonth]);
techStartDatesToShow.push(techSet[(currentMonth + 1)]);
}
let pswStartDatesToShow = [];
if (currentDate > pswCutOffDates[currentMonth]) {
pswStartDatesToShow.push(pswSet[(currentMonth + 1)]);
pswStartDatesToShow.push(pswSet[(currentMonth + 2)]);
}
else {
pswStartDatesToShow.push(pswSet[currentMonth]);
pswStartDatesToShow.push(pswSet[(currentMonth + 1)]);
}
// Assign start date sets to pages:
switch ({post_id}) {
case 96:
document.getElementById("start-date-1").innerHTML = phrmStartDatesToShow;
break;
case 1420:
document.getElementById("start-date-1").innerHTML = pswStartDatesToShow;
break;
case 1411:
case 1422:
document.getElementById("start-date-1").innerHTML = techStartDatesToShow;
break;
default:
document.getElementById("start-date-1").innerHTML = monthlyStartDatesToShow;
break;
}
monthlyStartDatesToShow is working great, but the others are not. They either display only one date (which is incorrect, December), or neither.