Как отформатировать массив расписания с начала и окончания времени в for () в JavaScript? - PullRequest
0 голосов
/ 30 января 2019

Моя цель - запустить таймер в назначенное время (что он и делает), но я не хочу, чтобы даты (29 января 2019 г.) Я просто хочу, чтобы время не обновлялось.каждый день.Любые предложения о том, как я могу изменить, чтобы for () работал для времен, а не для дат?

Я попытался удалить только дату и оставить время, и оно не будет работать.

var schedule = [
    ['Jan 29, 2019 20:43:00', 'Jan 29, 2019 21:48:05']
]
<!-- Mainly this part -->
for(var i=0; i<schedule.length; i++){
  var startDate = schedule[i][0];
  var endDate = schedule[i][1];

  // put dates in milliseconds for easy comparisons
  var startMs = Date.parse(startDate);
  var endMs = Date.parse(endDate);
  var currentMs = Date.parse(new Date());

  // if current date is between start and end dates, display clock
   if(endMs > currentMs && currentMs >= startMs ){
      initializeClock('clockdiv', endDate);
      openRequestedPopup();
        myStopFunction();
        setInterval(function(){window.location.reload(5);}, 306000);
        setTimeout(function () { windowObjectReference.close();}, 305000);

}

Отображать только таймер с датой и временем, и я просто хочу, чтобы таймер отображал только время начала и окончания.

Ответы [ 2 ]

0 голосов
/ 30 января 2019
var schedule = [
['Jan 30, 2019 10:40:00', 'Jan 30, 2019 12:12:05']
];
for(var i=0; i<schedule.length; i++){
var startDate = schedule[i][0];
var endDate = schedule[i][1];
 // put dates in milliseconds for easy comparisons
var startMs = Date.parse(startDate);
var endMs = Date.parse(endDate);
var currentMs = Date.parse(new Date());

// if current date is between start and end dates, display clock
 if( currentMs >= startMs && currentMs<endMs){ 


  var id =    setInterval(function(){ printTime(); }, 1000);

  function printTime(){
  var time = new Date();
  currentMs = time.getTime();
  var hours = time.getHours();
  var minutes = time.getMinutes();
  var seconds = time.getSeconds();
  document.write(hours + " " + minutes+" " + seconds+" ");


  if(currentMs>=endMs)
    clearInterval(id);  
 }
}

}

отметьте это, это может быть полезно для вас ... продолжайте учиться .....

0 голосов
/ 30 января 2019

Исходя из того, что я прокомментировал, в таком случае должно работать что-то подобное:

const schedule = [
    ['Jan 29, 2019 20:43:00', 'Jan 29, 2019 21:48:05']
]

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() ){
  	// Whatever we are doing when it's inside the window
  }
  
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...