Vinny
Vinny

Reputation: 75

Display text "A Day" or "B Day" every other day, regardless of current date

I am trying to display the text "A Day" on one day, then "B Day" the next. I can do this with the even or odd function then configure getDay object, but i am trying to do this regardless of the day, month or year.

For example, today could be "A Day" tomorrow is "B Day" then the next day is an "A Day" and so on and so forth.

I have started some code, but don't quite understand concept.

<html>

<body>
    <p id="demo"></p>
    <script type="text/javascript">
    myFunction();

    function myFunction() {
        var time = new Date().getDate();
        if (time == 0) {
            greeting = "A Day";
        } else {
            greeting = "B Day";
        }
        document.getElementById("demo").innerHTML = greeting;
    }
    </script>
</body>

</html>

Upvotes: 0

Views: 941

Answers (4)

gurpreet singh
gurpreet singh

Reputation: 93

You can do it this way:

var aDayBDayArray = ['A Day', 'B Day']

function printDay(aDayBDayArray) {
    console.log(aDayBdayArray[0]);
    aDayBDayArray.reverse();
}                                                                            

Store aDayBDayArray in a cookie or browser local storage or add this logic on server side. Use printDay() method once a day to get work done

You can configure it further:

function printDay(aDayBDayArray, swapDays = True) {
    console.log(aDayBdayArray[0]);
    if (swapDays) {
        aDayBDayArray.reverse();
    }
}

If you want more number of days to be included you can do it like

  daysArray = ['a day', 'b day', 'c day', ......... 'n day']

then circularly rotate daysArray in the above logic.

Upvotes: 0

user8198431
user8198431

Reputation:

First of all: the getDate() method return the current day of the month, for example, if today is the 25th, your variable 'time' will be 25. So the if (time==0) is no sense because there will never be a 0 day.

For the second problem, wich is that you want to extabilish the a day and b day, you could try to declare a counter wich is incremented when the date is even or odd:

var counter =0;
function myFunction() {

  if (counter%2 == 0) {
      greeting = "A Day";
      counter++;
  } else {
      greeting = "B Day";
      counter++;
  }
}

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22564

You can keep record of a day in a year and the find if the day is odd and even and display the string accordingly.

myFunction();
function myFunction() {
  var now = new Date();
  var start = new Date(now.getFullYear(), 0, 0);
  var diff = now - start;
  var oneDay = 1000 * 60 * 60 * 24;
  var day = Math.floor(diff / oneDay);
  if (day % 2  == 0) {
      greeting = "A Day";
  } else {
      greeting = "B Day";
  }
  document.getElementById("demo").innerHTML = greeting;
}
<p id="demo"></p>

Source : StackOverflow

Upvotes: 1

Matthew Stanley
Matthew Stanley

Reputation: 26

I suspect what's occurring is that you are always getting B Day, correct?

Calling getDate on a date object is going to return an integer value of the day. You're checking to see if that day of the month is 0. This does not start at offset 0 (the first day of the month is 1). Therefore, there shouldn't ever be a case where "A day" should come back.

I would not recommend using the day of the month as I don't think that would be reliable. The best option I could see is doing modulo division (checking for a remainder) and dividing by 2. If the remainder is 0, show A, if the remainder is 1, show B.

However, this becomes a problem with months that end in 31 days. 31 / 2 has a remainder of 1. The first day of the month, 1 /2, also has a remainder of one, creating two "B days".

My first thought is to see if you could somehow store a local variable that you change, or use a difference reference variable.

Upvotes: 0

Related Questions