0

I have to compare two Dates in JS, representing the beginning of the next session. All Sessions are saved in a String array. If the actualSession is over,nextSession shall be the actual Session and the nextSession becomes the first element of the array, then it is shifted off. But the comparison of the Dates does not work. Can you help me?

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();


}
Seppl22
  • 13
  • 2
  • Possible duplicate of [Compare two dates in JS](https://stackoverflow.com/questions/7988525/compare-two-dates-in-js) – prasanth Jul 11 '17 at 08:02
  • 1
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Kukic Vladimir Jul 11 '17 at 08:02
  • This is not a duplicate concerning dates. The comparison is done fine. This problem is not related to dates in general. @OP i dont get, why do you set actual_session and next_session to a static date ( not related to the array)? – Jonas Wilms Jul 11 '17 at 08:24
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Jonas Wilms Jul 11 '17 at 08:28

2 Answers2

0

The problem is that youre declaring your array and variables inside the function. So it is reset at every call, move it outside:

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(){...}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Because of the local declaration of variables in getNextSession method, the values are not retained

  • html Next session

-js

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());
}

working fiddle https://jsfiddle.net/sainadhvemula/rmw36r90/6/