Conditional statement in JavaScript with timer functionality

I have an existing 'if else' statement that functions correctly, but now I am seeking to add another condition to it. The initial statement is displayed below:

function showDateContent(){
 var d=new Date();
  if(d.getDate()>=11 && d.getMonth()+1 == 11){
   //display something
 }else{
   //display something else
    }
  }

showDateContent();

The new conditional statement I am attempting to write is as follows (below), however, it does not seem to be working. In this case, I am trying to trigger actions based on the date falling within specific ranges in the first statement, followed by two additional conditions similar to the first example.

function showDateContent(){
 var d=new Date();
  if(d.getDate() > 11 && d.getDate() < 13 && d.getMonth()+1 == 11){
   //display content for dates between 11 and 13
} else if(d.getDate() >= 14 && d.getMonth()+1 == 11){
   //display content for dates after the 14th
  }else{
   //display alternative content for dates before the 11th
    }
  }
}

showDateContent();

Answer №1

Consider using console.log() to display the current date and month in your browser, then implement if and else statements based on this information.

Currently, it is November 13, 2017 in India and your code does not account for the date being equal to 13, causing it to execute the last else statement.

function showDateContent(){
 var d=new Date();
console.log(d.getDate(), d.getMonth());   // prints 13   10
  if(d.getDate()> 11 && d.getDate()< 13 && d.getMonth()+1 == 11){
   console.log("Display something when the date falls between 11 and 13");
  } else if(d.getDate()>=14&& d.getMonth()+1 == 11){
   console.log("Display something when the date is on or after the 14th");
  } else {
   console.log("Display something different when the date is before the 11th");
   }
}

showDateContent();

By changing d.getDate()<= 13, the code now correctly enters the specified if block as shown below:

if(d.getDate()> 11 && d.getDate()<= 13 && d.getMonth()+1 == 11){
 console.log("Display something when the date is between 11 and 13");
}

Furthermore, there was an unnecessary "}" bracket at the end of your code which has been removed.

Answer №2

Start by verifying the accuracy of the month

if(d.getMonth() + 1 == 11) {
...
}

Next, consider the following three scenarios:

if(d.getDate() < 11) {

} else if(d.getDate() > 13) {

} else {

}

Combine these conditions as follows:

 if(d.getMonth() + 1 == 11) {

       if(d.getDate() < 11) {

       } else if(d.getDate() > 13) {

       } else {

       }
    }

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

Is it possible to permanently alter HTML values with JavaScript?

Can a html value be permanently modified using javascript? I am trying to edit a local file. Below are the codes I'm using: function switchPic(){ top.topPage.activeDef = top.topPage.document.getElementById('h1'); top.topPage.activeDef. ...

Tips for utilizing the 'contains' feature within web elements with Java Selenium WebDriver?

In my current project, I am facing a challenge with two specific elements: one is a string formatted as ABC £12,56 and the other is a box called "Cashbox" that should be 15% of the value of the string. However, when attempting to locate the CSS for both e ...

"Immersive Virtual Reality Experience with Curved Plane and Interactive Text in

While the Aframe API already supports curved images, I'm interested in creating curved text and planes as well. Can anyone provide guidance on how to achieve this? I believe I need to create an entity for this purpose, but I am unsure about which pro ...

Binding Models Dynamically with VueJS

We are currently exploring the use of VueJs 2.0 for our newest project, but we have encountered an early obstacle that we hope to resolve quickly! Our project involves creating over 150 form fields using Laravel, and we want to bind these parameters to Vu ...

Modify the size of images while shuffling using Javascript

Hey there! I've got some bootstrap thumbnails set up and I'm using a script to shuffle the images inside the thumbnails or a element within the li. It's working fine, but some of the images are coming out larger or smaller than others. I&apo ...

Modifying the names of JavaScript variables and functions by leveraging Rhino or Closure Compiler in the Java environment

I am looking for a way to dynamically change variable and function names in a JavaScript file under certain conditions, similar to how a JavaScript minifier operates. Here is an example: I start with the following JavaScript code: var something=5; somethi ...

Conceal specific elements across various paths in Angular 5

Attempting to hide a component based on matching routes, it is currently working with one route but not with multiple routes. import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ select ...

Utilize Vue to activate updates by customizing a d3 event

Summary: I am working on a Vue.js app where multiple components need to update after a property change triggered by a d3 event dispatch. However, only the last component seems to react. In my app, there is a component called Loc that contains 23 d3 visual ...

What is the best way to convert a text structure into JSON using regular expressions?

I was provided with a text that follows this particular structure: #object_name object_id action_name param1 ... paramN module action_name_1 param1 ... paramK action_name_N param1 ... paramM module_end some_other_action_name param1 ... paramJ #som ...

Angular2: dynamic spinner directive for showing progress/loading overlay

Struggling to implement a loading indicator/overlay in Angular2 that can be added to any container div. When the boolean property isLoading changes, I want the div to grey out and display a spinning indicator, then revert back when the property changes. I ...

The protected routes within a React JS application consistently result in an undefined value

In my ProtectedRoute component for my react application, I am facing an issue with user authentication. I am using react-router-dom v6 to access the token from localStorage, but no matter what, the user variable always returns undefined. import { Outlet, N ...

Pass information from the controller to the view in AngularJS

As I am still relatively new to Angular, I believe I might be overlooking a crucial step in transferring data from the controller to the view for display. The specific functionality I am trying to achieve involves a user entering search criteria into a fo ...

The challenge of initiating and handling a database request

Whenever I send requests using Postman for creating a product (or other requests such as GET, PUT, etc.), I encounter the following error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'create' of undefined I'm strugglin ...

Steps for exporting all elements of an array to an Excel file using the exceljs library in Node.js

I am facing an issue where I need to export all elements from an array to an Excel file using exceljs in Node.js. Here is an example of my array structure: array image After defining my array, the goal is to create an Excel file that looks like this: exce ...

Command for kicking initiated, tag unreadable

I'm currently working on adding a new command to my bot, but I can't seem to figure out why the 'tag' property is showing as undefined. While ${user.tag} works perfectly fine in my real kick command, it fails to recognize the user who s ...

java code unicode feature in csharp

Here's the code I am using: $(document).ready(function () { var breadCrumps = $('.breadcrumb'); breadCrumps.find('span').text("<%= ArticleSectionData.title %>"); }); The 'title' property contains values en ...

Steps to toggle text color upon clicking and switch the color of another text

As a beginner with no Js knowledge, I am trying to achieve a specific function. I want to be able to change the color of text when it is clicked, but also have the previously clicked text return to its original color. <div class="mt-5 fi ...

Removing and shattering data from a JSON file at a designated location in AngularJS

I have received json data that is structured like this (and unfortunately cannot be modified): Desc: First data - Second data My current method of displaying this data involves using the following code: <div ng-repeat="b in a.Items">{{b.Desc}}< ...

Cannot Get jQuery .flip() to Work Automatically Every 2 Seconds

There are 3 words that I want to rotate on their x-axis every 2 seconds (repeating). Using jQuery: $(function () { count = 0; wordsArray = ["Innovation", "Creativity", "Success"]; setInterval(function () { count++; $("#words").text(wordsArray[co ...

In Bootstrap 3, you can toggle individual collapsible items without affecting others by only closing the specific

I am facing an issue with my Bootstrap collapsible elements. I came across an answer that includes the necessary javascript to close only the collapsible items in my table, while leaving other collapsible items on the page (in the menu) unaffected. These i ...