如何在不同的日子使用不同的数组?

时间:2019-01-31 20:49:03

标签: javascript arrays

我为校铃时间表设置了两个日期和时间数组。学校在星期三使用不同的钟表时间表。我如何只在星期三将阵列拉到星期三,并允许其他阵列在星期三以外的每一天正常工作?该代码将采用数组中的开始和结束时间,如果时间在开始和结束时间之间,则将花费两者之间的时间并显示倒数计时器。

//start of code
var d = new Date(2019);
function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  return {
    'total': t,
    'minutes': minutes,
    'seconds': seconds
  };
}
var myVar;


    myVar = setInterval(function(){window.location.reload(1);}, 1000);


function myStopFunction() {
  clearTimeout(myVar);
}

function initializeClock(id, endtime) {
  var clock = document.getElementById(id);
  clock.style.display = 'block';
  var minutesSpan = clock.querySelector('.minutes');
  var secondsSpan = clock.querySelector('.seconds');

  function updateClock() {
    var t = getTimeRemaining(endtime);



    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
 }



  updateClock();
  var timeinterval = setInterval(updateClock, 1000);

}

var windowObjectReference;

function openRequestedPopup() {
  windowObjectReference = window.open(
    "file:///C:/Users/Darren%20Blount/OneDrive/COUNTDOWN/5_timer.html", 
"DescriptiveWindowName", 'height=30000,width=40000, left=40000, resizable=yes,scrollbars=yes,toolbar=yes, status=yes');


var monthNames = ["January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", 
"December"];
var d = new Date();
var currentMonth = monthNames[d.getMonth()];

var DayNames = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
"24", "25", "26", "27", "28", "29", "30", "31"];
var g = new Date();
var currentDay = DayNames[g.getDay()];



var wednesday /* Havenot worked into code*/= [
    // Daily Bell Schedule
    ['Oct 30, 2018 7:50:49', 'Oct 30, 2018 8:00:49'],
    //AAP
    ['Oct 30, 2018 9:29:49', 'Oct 30, 2018 9:34:49'],
    //1st Block - 2nd Block
    ['Oct 30, 2018 9:58:49', 'Oct 30, 2018 10:03:49'],
    //2nd Block - 1st Half 3rd Block

    ['Oct 30, 2018 11:49:49', 'Oct 30, 2018 11:37:49'],
    //1st Lunch - 3rd Block
    ['Oct 30, 2018 12:02:49', 'Oct 30, 2018 12:07:49'],
    //2nd Lunch - 2nd Half of 3rd Block
    ['Oct 30, 2018 12:42:49', 'Oct 30, 2018 12:47:49'],
    //3rd Block - 4th Block
    ['Oct 30, 2018 13:36:49', 'Oct 30, 2018 13:41:49']
];

const schedule /*Everyday*/= [
    ['Sep 11, 2018 7:50:49', 'Sep 11, 2018 8:00:49'],
    //AAP
    ['Sep 11, 2018 9:29:49', 'Sep 11, 2018 9:34:49'],
    //1st Block - 2nd Block
    ['Sep 11, 2018 9:58:49', 'Sep 11, 2018 10:03:49'],
    //2nd Block - 1st Half 3rd Block
    ['Sep 11, 2018 11:49:49', 'Sep 11, 2018 11:37:49'],
    //1st Lunch - 3rd Block
    ['Sep 11, 2018 12:02:49', 'Sep 11, 2018 12:07:49'],
    //2nd Lunch - 2nd Half of 3rd Block
    ['Sep 11, 2018 12:42:49', 'Sep 11, 2018 12:47:49'],
    //3rd Block - 4th Block
    ['Sep 11, 2018 1:36:49', 'Sep 11, 2018 1:41:49']
];


const schedule = [
    [currentMonth + currentDay + '2019 13:40:00', currentMonth + currentDay 
+ '2019 14:30:00']
]

for(let i=0; i<schedule.length; i++){
// pull them straight into Date objects
const startDate = new Date(schedule[i][0]);
const endDate = new Date(schedule[i][1]);

// Make a new Date for setting it for today, then set the hours based off 
the schedule
let startTime = new Date();
startTime.setHours(startDate.getHours(), startDate.getMinutes(), 
startDate.getSeconds());
let endTime = new Date();
endTime.setHours(endDate.getHours(), endDate.getMinutes(), 
endDate.getSeconds());

// Easier way to just get the ms and then the same check
const currentMs = Date.now();
if(endTime.valueOf() > currentMs && currentMs >= startTime.valueOf() ){
    initializeClock('clockdiv', endDate);
      openRequestedPopup();
        myStopFunction();
        setInterval(function(){window.location.reload(5);}, 306000);
        setTimeout(function () { windowObjectReference.close();}, 305000);
        }
}

1 个答案:

答案 0 :(得分:0)

要获得预期的结果,请使用getDay()获取当前日期的以下选项 (从周日开始-0,周三为3)

var d = new Date();
var today = d.getDay();

var wednesday = [
    // Daily Bell Schedule
    ['Oct 30, 2018 7:50:49', 'Oct 30, 2018 8:00:49'],
    //AAP
    ['Oct 30, 2018 9:29:49', 'Oct 30, 2018 9:34:49'],
    //1st Block - 2nd Block
    ['Oct 30, 2018 9:58:49', 'Oct 30, 2018 10:03:49'],
    //2nd Block - 1st Half 3rd Block

    ['Oct 30, 2018 11:49:49', 'Oct 30, 2018 11:37:49'],
    //1st Lunch - 3rd Block
    ['Oct 30, 2018 12:02:49', 'Oct 30, 2018 12:07:49'],
    //2nd Lunch - 2nd Half of 3rd Block
    ['Oct 30, 2018 12:42:49', 'Oct 30, 2018 12:47:49'],
    //3rd Block - 4th Block
    ['Oct 30, 2018 13:36:49', 'Oct 30, 2018 13:41:49']
];

var everyday = [
    ['Sep 11, 2018 7:50:49', 'Sep 11, 2018 8:00:49'],
    //AAP
    ['Sep 11, 2018 9:29:49', 'Sep 11, 2018 9:34:49'],
    //1st Block - 2nd Block
    ['Sep 11, 2018 9:58:49', 'Sep 11, 2018 10:03:49'],
    //2nd Block - 1st Half 3rd Block
    ['Sep 11, 2018 11:49:49', 'Sep 11, 2018 11:37:49'],
    //1st Lunch - 3rd Block
    ['Sep 11, 2018 12:02:49', 'Sep 11, 2018 12:07:49'],
    //2nd Lunch - 2nd Half of 3rd Block
    ['Sep 11, 2018 12:42:49', 'Sep 11, 2018 12:47:49'],
    //3rd Block - 4th Block
    ['Sep 11, 2018 1:36:49', 'Sep 11, 2018 1:41:49']
];

if(today ===3){
  console.log("Wednesday", wednesday)
}else{
  console.log("not wednesday", everyday)
}