Search code examples
javascriptjquerycountdowncountdowntimerjquery-countdown

jQuery delivery countdown - hide in specific time in weekends


I am currently working on an e-commerce site, where we need a delivery counter. This is what i currently have working, where the counter hides in the weekends:

function twoD(n){
    return (n < 10 ? "0" : "") + n;
}
$(document).ready(function () {
    setInterval(function () {
        var now = new Date();
        var day = now.getDay();
        var end;

        if(day >= 1 && day <= 5) {
            end = new Date(now.getYear(), now.getMonth(), day, 14, 0, 0, 0);    
        } else {
            $('#wc_countdowntimer').hide();
        }

        var timeleft = end.getTime() - now.getTime();
        var diff = new Date(timeleft);


        $("#wc_countdowntimer").html("<i class='fa fa-clock-o'></i> Next shipment in " + twoD(diff.getHours()) + ":" + twoD(diff.getMinutes()) + ":" + twoD(diff.getSeconds()) + "");

    }, 1000);
});

The only problem is, that we want the counter to be hidden already after 14PM on friday, and then displayed again on sunday after 14PM. This will make the counter only visible when there is less than 24 hours to the order will be shipped out.

I have tried making various changes, but I can't get the code working the way I want to.

Hopefully someone in here, can help. Any help will be appreciated.


Solution

  • You can do it like this:

     function isVisibleByDate(date) {
       var isVisible = true;
       //  hidden already after 14PM on friday and before sunday after 14PM
       var hour = date.getHours();
       var day = date.getDay();
       if ((day == 5 && hour > 14) || (day == 6 && hour < 14)) isVisible = false;   
    
       return isVisible;
     }
    

    Online demo (jsFiddle)