Business Hours Open Close

User
We have this type time stores now want to show open or close based on day and open and close time jquery
Assistant
You can use jQuery to check the current day and time, then compare it with the stored values to determine whether the business is open or closed. Here’s how you can do it:

### Solution:

$(document).ready(function () {
    function checkBusinessHours() {
        let currentDate = new Date();
        let currentDay = currentDate.toLocaleString("en-US", { weekday: "long" }); // Get current day
        let currentTime = currentDate.getHours() * 60 + currentDate.getMinutes(); // Get current time in minutes

        $(".business_hours_time").each(function () {
            let openTime = $(this).data("opentime");
            let closeTime = $(this).data("closetime");
            let day = $(this).data("day");

            if (day === currentDay) {
                let openMinutes = parseInt(openTime.split(":")[0]) * 60 + parseInt(openTime.split(":")[1]);
                let closeMinutes = parseInt(closeTime.split(":")[0]) * 60 + parseInt(closeTime.split(":")[1]);

                if (currentTime >= openMinutes && currentTime <= closeMinutes) {
                    $("#status").text("Open");
                } else {
                    $("#status").text("Closed");
                }
            }
        });
    }

    checkBusinessHours(); // Run function on page load

    setInterval(checkBusinessHours, 60000); // Update status every minute
});

### HTML:

<input type="hidden" class="business_hours_time" data-closetime="17:00" data-opentime="08:00" data-day="Monday">
<div id="status"></div>

### Explanation:
1. The script retrieves the current day and time.
2. It loops through each `.business_hours_time` element to find the matching day.
3. It compares the current time with the opening and closing times.
4. If the current time falls within business hours, it displays "Open"; otherwise, it shows "Closed".
5. It updates every minute to keep the status accurate.

Let me know if you need modifications! 🚀

User
not use setInerval
just run one time
business-hours-widget and run udner div business-hours-widget
Assistant
Here’s an updated version of your jQuery script that runs only once, without using `setInterval`, and displays the status inside a `.business-hours-widget` div.

### **Solution:**

$(document).ready(function () {
    function checkBusinessHours() {
        let currentDate = new Date();
        let currentDay = currentDate.toLocaleString("en-US", { weekday: "long" }); // Get current day
        let currentTime = currentDate.getHours() * 60 + currentDate.getMinutes(); // Get current time in minutes

        $(".business-hours-widget").each(function () {
            let statusElement = $(this).find(".status"); // Find the status element inside the widget

            $(this).find(".business_hours_time").each(function () {
                let openTime = $(this).data("opentime");
                let closeTime = $(this).data("closetime");
                let day = $(this).data("day");

                if (day === currentDay) {
                    let openMinutes = parseInt(openTime.split(":")[0]) * 60 + parseInt(openTime.split(":")[1]);
                    let closeMinutes = parseInt(closeTime.split(":")[0]) * 60 + parseInt(closeTime.split(":")[1]);

                    if (currentTime >= openMinutes && currentTime <= closeMinutes) {
                        statusElement.text("Open");
                    } else {
                        statusElement.text("Closed");
                    }
                }
            });
        });
    }

    checkBusinessHours(); // Run function on page load
});

### **HTML Structure:**

<div class="business-hours-widget">
    <input type="hidden" class="business_hours_time" data-closetime="17:00" data-opentime="08:00" data-day="Monday">
    <div class="status"></div>
</div>

### **How It Works:**
– Finds all `.business-hours-widget` divs.
– Looks for `.business_hours_time` inside each widget.
– Compares the current day and time with the stored business hours.
– Updates the `.status` div inside the widget to display **"Open"** or **"Closed"**.

Let me know if you need modifications! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *