How to show Open/Closed based on time records with an html class

后端 未结 1 662
天命终不由人
天命终不由人 2021-01-18 14:36

I\'m trying to display Open or Closed based on a company\'s time for that specific date using Javascript. I\'m using a theme Listify on WordPress which customers can list th

相关标签:
1条回答
  • 2021-01-18 15:23

    I've modified your code a little, making use of date.js:

    http://codepen.io/anon/pen/VaGdBK

    var da = new Date();
    document.getElementById("display").innerHTML = da.toDateString();
    
    
    
    
    //gets the current time. 
    //var d = new Date();
    var x = document.getElementsByClassName("start")[0].innerText;
    var z = document.getElementsByClassName("end")[0].innerText;
    //var o = parseInt(x,10);
    //var c = parseInt(z,10);
    
    var startTime = Date.parseExact(x, "h:mm tt");
    var endTime = Date.parseExact(z, "h:mm tt");
    
    if (da.between(startTime, endTime)){
        $(".open").show();
        $(".closed").hide();
    }
    else {  
        $(".closed").show();
        $(".open").hide();
    }
    .open, .closed {
      display: none;
    }
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>
    <span class="day">Monday </span>
    <span class="start">8:30 am</span> - 
    <span class="end">5:30 pm</span>
    <br>
    <span id="display"></span>
    <div class="open">Shop is open</div>
    <div class="closed">Shop is closed</div>

    I haven't looked into it sufficiently so I don't know how it works regarding the timezone. You might have to adjust what I've posted to account for that.

    Also: added a CSS rule to avoid the brief flash of both open and closed being displayed before one of them is hidden by jQuery.

    0 讨论(0)
提交回复
热议问题