Home>

The content switching by time was successful using "getHours".
I'd like to switch to the following hour and minute, but it doesn't work.
I am very sorry for the rudimentary question, but I would appreciate it if you could teach me.


・ 17: 45⇒ Content A display
・ 17: 46⇒ Content B display

$(function () {
    var now_hour = new Date (). getHours ();
    var now_minutes = new Date (). getMinutes ();
      // var now_hour = new Date (). getHours ();
    if (16<= now_hour&&now_hour<= 18, now_minutes<= 52) {
        document.getElementById ('popup_write2'). innerHTML = 'Content A';
    } else if (18<= now_hour, now_minutes<= 53&&now_hour<= 0) {
        document.getElementById ('popup_write2'). innerHTML = 'Content B';
    } else if (0<= now_hour&&now_hour<= 9) {
        document.getElementById ('popup_write2'). innerHTML = 'Content C';
    }
});
  • Answer # 1

    I feel too difficult.

    var now = new Date ();
    var hour = now.getHours ();
    var min = now.getMinutes ();
    var time = hour + "" + ('00' + min) .slice (-2);
    if (time>= 1746) {
      alert ('aiueo');
    } else {
      alert ('kakikukeko');
    }

    Sample

  • Answer # 2

    If it is there, "," is not necessary, so combine&&|| () etc.
    If you want to change it in real time, setTimeout () is also
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Logical_Operators
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Grouping

    https://developer.mozilla.org/en/docs/Web/API/WindowTimers/setTimeout

  • Answer # 3

    Based on additional notes.
    As I mentioned in the revised section, the comma operator is meaningless, so I usually use and and parentheses.
    Or I think you can simply display the time in four digits, as with asuchi0819. It ’s easier to see.

    var now_hour = new Date (). getHours ();
        var now_minutes = new Date (). getMinutes ();
        if ((10<= now_hour)&&(now_hour<18)) {// 10 o'clock to 17:59
            document.getElementById ('popup_write2'). innerHTML = 'Content A';
        } else if ((18<= now_hour)&&(now_minutes<= 19 || (now_minute == 20&&now_hour<= 45))) {// 18 hour-19:59, 20 hour 0 minute-20:45 Min
            document.getElementById ('popup_write2'). innerHTML = 'Content B';
        } else if (0<= now_hour&&now_hour<9 || (now_minute == 20&&now_hour>45) || now_minute>20) {// 0: 0 to 8:59, 20:46 to 20:00 59 minutes, 21:00
            document.getElementById ('popup_write2'). innerHTML = 'Content C';
        }
       // The commented out "~" is "&&", and "is the if statement corresponding to" || "

    ... I thought after writing, but asuchi0819 might be better. It ’s hard to see if you break it up. .

  • Answer # 4

    The source suggested by asuchi0819 was used.
    Because the time zone that I want to realize happened to be "digits" double digits,
    The behavior was confirmed even with "var time = hour +" "+ min;".
    I will add the description below.

    var now = new Date ();
        var hour = now.getHours ();
        var min = now.getMinutes ();
        // var time = hour + "" + min;
        var time = hour + "" + ('00' + min) .slice (-2);
        if (time<= 859) {
          // 08: Display up to 59:59 (Content B)
          alert ('display until 8:59');
        } else if (time<= 1759) {
          alert ('');
          // display nothing until 17:59
        } else if (time<= 2045) {
          // Display after 20:45 (content A)
          alert ('display after 20:45');
        } else {
          // Display after 20:46 (Content B)
          alert ('display after 20:46');
        }