Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较JavaScript中的两个特定日期_Javascript - Fatal编程技术网

比较JavaScript中的两个特定日期

比较JavaScript中的两个特定日期,javascript,Javascript,我必须比较JS中的两个日期,代表下一个会话的开始。所有会话都保存在字符串数组中。如果actualSession已结束,则nextSession应为实际会话,并且nextSession将成为数组的第一个元素,然后将其移开。但是,日期的比较不起作用。你能帮我吗 function initializeComparison(){ getNextSession(); window.setInterval("getNextSession()", 15000); } function getNextSe

我必须比较JS中的两个日期,代表下一个会话的开始。所有会话都保存在字符串数组中。如果actualSession已结束,则nextSession应为实际会话,并且nextSession将成为数组的第一个元素,然后将其移开。但是,日期的比较不起作用。你能帮我吗

function initializeComparison(){
getNextSession();
window.setInterval("getNextSession()", 15000);
}



function getNextSession(){
var actual_session = new Date(2017, 6, 22, 17,00);
var next_session = new Date(2017, 7, 6, 17, 00);
var allSessionsString = new Array("September 16, 2017 17:00:00", "September 
30, 2017 17:00:00"); //more to come, just for example

if(actual_session < next_session){
actual_session = next_session;
next_session = new Date(allSessionsString[0]);
allSessionsString.shift();

}

var element = document.getElementById("nextSession");
element.innerHTML = "Next Session: " + actual_session.toLocaleString();


}
函数初始化比较(){
getNextSession();
setInterval(“getNextSession()”,15000);
}
函数getNextSession(){
var实际会议=新日期(2017年6月22日17月00日);
var下次会议=新日期(2017年7月6日17日00分);
var allsessionstring=新数组(“2017年9月16日17:00:00”,“9月
2017年12月30日17:00:00“;//还有更多,举个例子
if(实际会话<下一会话){
实际会话=下一个会话;
下一个会话=新日期(AllSessionString[0]);
allsessionstring.shift();
}
var元素=document.getElementById(“nextSession”);
element.innerHTML=“下一个会话:”+actual_Session.toLocaleString();
}

问题在于您在函数中声明了数组和变量。因此每次通话时都会重置,将其移出:

var actual_session = new Date(2017, 6, 22, 17,00);
var next_session = new Date(2017, 7, 6, 17, 00);
var allSessionsString =["September 16, 2017 17:00:00","September 30, 2017 17:00:00"]; 

 function getNextSession(){...}

由于getNextSession方法中变量的局部声明,因此不会保留这些值

  • html 下一届会议

-js

函数初始化比较(){
实际会议=新日期(2017年6月22日17月00日);
下一届会议=新日期(2017年7月6日17时);
AllSessionString=new Array(“2017年9月16日17:00:00”,“2017年9月30日17:00:00”);//更多内容,仅举个例子
refreshIntervalId=window.setInterval(“getNextSession()”,5);
}
函数getNextSession(){
if(实际会话<下一会话){
实际会话=下一个会话;
下一个会话=新日期(AllSessionString[0]);
if(typeof allsessionstring[0]=“未定义”){clearInterval(refreshIntervalId);}
allsessionstring.shift();
}
var元素=document.getElementById(“nextSession”);
element.append(“下一个会话:+actual_Session.toLocaleString());
}

工作小提琴

可能的重复可能的重复这不是关于日期的重复。比较做得很好。这个问题一般与日期无关@OP i dont get,为什么要将实际会话和下一个会话设置为静态日期(与数组无关)?可能重复
function initializeComparison(){
 actual_session = new Date(2017, 6, 22, 17,00);
 next_session = new Date(2017, 7, 6, 17, 00);
 allSessionsString = new Array("September 16, 2017 17:00:00", "September 30, 2017 17:00:00"); //more to come, just for example
 refreshIntervalId = window.setInterval("getNextSession()", 5);
}



function getNextSession(){

if(actual_session < next_session){
actual_session = next_session;
next_session = new Date(allSessionsString[0]);
if(typeof allSessionsString[0] == "undefined"){clearInterval(refreshIntervalId);}
allSessionsString.shift();
}
var element = document.getElementById("nextSession");
element.append ("Next Session: " + actual_session.toLocaleString());
}