Tips for displaying a div at the right time and day of the week

Is there a way to toggle the visibility of a div based on specific times and days of the week? For example, showing div1 at 12:00 pm on Sundays and div2 at 12:00 pm on Mondays, while keeping them hidden at other times. I have code that currently handles the hide/show functionality based on time, but I would like to also incorporate weekday logic using something like

 if (day == Sunday || n > '08:59' && n < '09:59') {}

        function toggleDiv() {
        Object.prototype.twoDigits = function () {
            return ("0" + this).slice(-2);
        }

        // get current date and time
        let now = new Date();
        n = now.getHours().twoDigits() + ':' + now.getMinutes().twoDigits();

        if (n > '08:59' && n < '09:59') {
            $(".class-1").addClass("now").removeClass("old");
            $('.class-1 .overlay').hide();
            $('.class-2 .overlay').show();
            $('.class-3 .overlay').show();
            $('.class-4 .overlay').show();
            $('.class-1 .overlay-2').hide();
            $('.class-2 .overlay-2').hide();
            $('.class-3 .overlay-2').hide();
            $('.class-4 .overlay-2').hide();
        }
        else if (n > '09:59' && n < '10:59') {
            $(".class-2").addClass("now").removeClass("old");
            $('.class-1 .overlay').hide();
            $('.class-2 .overlay').hide();
            $('.class-3 .overlay').show();
            $('.class-4 .overlay').show();
            $('.class-1 .overlay-2').show();
            $('.class-2 .overlay-2').hide();
            $('.class-3 .overlay-2').hide();
            $('.class-4 .overlay-2').hide();
        }
        else if (n > '10:59' && n < '11:29') {
            $('.class-1 .overlay').hide();
            $('.class-2 .overlay').hide();
            $('.class-3 .overlay').show();
            $('.class-4 .overlay').show();
            $('.class-1 .overlay-2').show();
            $('.class-2 .overlay-2').show();
            $('.class-3 .overlay-2').hide();
            $('.class-4 .overlay-2').hide();
        }   
       
        else {
            $(".class-1").addClass("old").removeClass("now");
            $(".class-2").addClass("old").removeClass("now");
            $(".class-3").addClass("old").removeClass("now");
            $(".class-4").addClass("old").removeClass("now");
            $('.hooray').show();
            $('.overlay').show();
            $('.overlay-2').hide();
        }
    }
    $(document).ready(function () {
        toggleDiv();
    });

Answer №1

To determine the current day of the week, you can utilize the code snippet const d = now.getDay(), which will provide a numerical value from 0 to 6 with 0 representing Sunday. You can then incorporate this in your conditional statements such as if(d === 3 && ...) to specifically check for Wednesday.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Height set to 100% of the container

Why is my header fixed at the top? My footer remains fixed at the bottom. And there is no need for scrolling. How can the #content fill 100% of the remaining space in the center? body{overflow-y:hidden;} footer { position: fixed; height: 50p ...

"Utilizing SignalR for Real-time Communication in ASP.NET WebForms

I found the need to incorporate SignalR library into one of my projects. So, I took on the task of creating a simple application to understand its functionality better. My app is based on several tutorials demonstrating how to create a chat room. The uni ...

Analyzing cookie data and utilizing jQuery for data presentation

Let's brainstorm. I'm currently working on a chat bar and have successfully implemented its functionality. However, I am facing a challenge in maintaining the continuity of the chat boxes while navigating through different pages on the website. ...

Creating artistic masterpieces on a digital canvas with the use of touch

After conducting some research, I am struggling to find a solution that allows touch screen users to draw on the canvas tag using raw JavaScript. Despite my best efforts, I keep hitting a dead end and my canvas remains unresponsive. The canvas is located ...

What is the best way to align two boxes horizontally, with one positioned directly beneath the other?

The code remains unchanged, the only difference is that it does not align properly with the box above. Thank you in advance. CSS #menu { margin-top:75px; min-width:19px; max-width:1920px; height:40px; background-color:#0F0; border:4px groove #F00; } #h ...

Tips on extracting values from an array of object properties using lodash

I am working with the following object: { Id: '11ea9563-1a4c-c21b-904f-00ff98e8d5ab', Email: 'Email', Password: { type: 'Buffer', data: [ Buffer value] }, roles: [ { Name: 'Developer', userroles ...

When trying to access a URL in a next.js application using the fetch() function, the router encountered an

I recently started working on a frontend project using Next.js v13.4 app router, with an additional backend. I have organized my routes and related functionalities in the api folder within the app directory. However, when I attempt to use the fetch() funct ...

What is the best way to filter intricate JSON based on a condition nested within the JSON

Managing a collection of complex objects that require filtering based on multiple conditions var myList= [ { "UserId": 1, "UserDetails": { "Department": [ { "Name": "dept1" ...

Capture the contents of a table cell and store it in the clipboard

Is there a way to create a hover copy button in a table cell without actually placing the button inside the <td> tags? The goal is to allow users to easily copy text from a specific column cell by hovering over it. function myFunction() { var c ...

JavaScript for Office Spreadsheet Titles

I'm having trouble fetching the names of sheets from an external Excel file, as I keep getting an empty array. async function retrieveSheetNames() { const fileInput = <HTMLInputElement>document.getElementById("file"); const fileReader ...

`The mobile viewport is displaying the webpage with double the width`

After spending several hours trying to optimize my website for mobile, I'm still unable to get it working properly. It seems like one of the elements is exceeding its container width, causing issues. Any assistance would be greatly appreciated. If yo ...

Using jQuery to reload the page following a PHP form submission

Currently facing an issue as I am trying to load the same page submitted by PHP, specifically in a comment section. After users submit their message, I want to display the existing comments along with the new one. The problem arises because I'm not u ...

Having trouble incorporating an API module into a nested component in Vue

I have encountered a perplexing issue when attempting to import an API module into a nested component within a Vue application. After simplifying the problem as much as possible, I created a codesandbox example to demonstrate it. The problem arises when ...

Adding a new field to an embedded document in MongoDB (without using an array)

My task involves updating a json document in mongodb by single field. Despite the simplicity if my data was in an object array, it's more complex due to objects within objects caused by early design choices and reasons. This is the structure of the s ...

Tips for modifying the size of your input

I'm currently utilizing components from and I'm looking to modify the length of the input box to match the black box seen here: https://i.sstatic.net/RWYTU.png In the image, you can observe that the input box should be equal in length to the b ...

Steps to programmatically obtain the code for a contentPlaceHolder

I am seeking a way to extract the code of a contentPlaceHolder utilizing various Html parsers. The challenge is that I require a url, which is not feasible due to it being a masterpage. Essentially, there is a select tag where you can choose an option, tr ...

Background Image Division (Twitter Bootstrap)

I am facing a challenge while creating a module with a span8 width and varying height. The div contains an image that dictates its height, with text overlaid on the image. My issue lies in preventing part of the image from getting cropped when Bootstrap re ...

Checking the Focus of an Element in Vue.js

Below is the code I am working with: <template> <div id="app"> <span contenteditable="true" spellcheck="false" style="width: 800px; display: block" :v-text="textEl&qu ...

Prevent secret select fields from being submitted within a form

In my interface, there are a couple of dropdowns where a user can select the first option and based on that selection, a new dropdown will appear. Currently, when posting the form, all the select dropdown values are included in the post data, even the hid ...

Error message encountered when submitting a form after receiving an AJAX response: VerifyCsrfToken Exception

I'm encountering an issue with my AJAX functionality that involves rendering a form with multiple input fields and a submit button. Here is the AJAX call: <script type="text/javascript"> $('#call_filter').click(function() { $.aja ...