Luke Luke - 2 months ago 19
Javascript Question

check time period condition is not available

functionality:

when user click on a button, it will check on the condition if the time is between 6pm to 11pm of the day. If the time condition is met, it will show page A, else of the time is other than between 6pm to 11pm, it will show page B.

Issue:

I need help/assistance to have the syntax on how to do a conditional check for time that is between 6pm to 11pm -> show pageA else -> show pageB. I am unfamiliar on how to create the conditional check on checking if the condition satisfy between the time of 6pm to 11pm and the else factor

Hence the functional code is as follows :



isDrive = false;

if (isDrive == true) {

//check if time is between 6-11pm, show PageA_Video page, else show PageB_Video page
if () {
$("#PageA_Video").show();
$("#PageB_Video").hide();
} else if () {
$("#PageA_Video").hide();
$("#PageB_Video").show();
}
} else if (isDrive == false) {
$("#PageB_Video").show();
$("#PageA_Video").hide();
}




Answer

Use Date to get the time.

isDrive = false;

if (isDrive == true) {

  //check if time is between 6-11pm, show PageA_Video page, else show PageB_Video page
  var d = new Date();
  var h = d.getHours();
  if (h>=18 && h<23) {
    $("#PageA_Video").show();
    $("#PageB_Video").hide();
  } else {
    $("#PageA_Video").hide();
    $("#PageB_Video").show();
  }
} else if (isDrive == false) {
  $("#PageB_Video").show();
  $("#PageA_Video").hide();
}