0

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>
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
Vinny
  • 75
  • 11
  • `I have started some code, but not quite understanding the concept.` What exactly do you not understand? To me, this code is self-explaining. I don't know what needs explaining. Could you explain what exactly your problem is – Thomas Jun 25 '17 at 17:34
  • `but i am trying to do this regardless of the day, month or year` then you might want to take a look at timestamps rather than dates. – Thomas Jun 25 '17 at 17:37
  • This code, i can make it work for like an even or odd day. I want a function to automatically change the text to an "A Day" today and then a "B Day" tomorrow. It's a pattern that can not be attained with an even/odd function for my situation. – Vinny Jun 25 '17 at 17:38
  • 1
    When you did this regardless of the day, how would you know the difference between today and tomorrow? – Bergi Jun 25 '17 at 17:39
  • Are you allowed to store the day value somewhere? If yes, you can just save the last day, and check if current day is different than last day value. – jitendragarg Jun 26 '17 at 05:06

4 Answers4

1

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

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

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++;
  }
}
0

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.

0

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.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • How would i get this function to print the text? It shows a blank screen in the browser. – Vinny Jun 25 '17 at 18:34
  • Use alert() instead of console.log and you will see the effect, i have provided the basic logic, you can modify it and use it as per your requirements :) – gurpreet singh Jun 26 '17 at 17:11