0

I am trying to place the time and date on to an HTML paragraph element. However, JavaScript is refusing to cooperate with me and will not print it to the element, but it will print to the console if you remove the last two lines of code and replace them with a console.log. I thought this would work as I have made very similar pieces of code. However, the code happens to NOT work. I put the entire piece of code into a JavaScript error detector. A missing semicolon. Easy fix. After placing the semicolon, it says something about a small compatibility warning and that’s it. I don’t care about compatibility as of now. I have:

  • tweaked it for the console (worked perfectly then)
  • put it into error detector (no critical errors)
  • change the link in my HTML head tag as it’s apparently supposed to be script tag and not link tag
  • add the MIME type of the JavaScript file into that new element

all to no avail of fixing the main code. However, on the bright side, it works with the developer console. Here’s the script that I have right now:

var detectTimeParagraph;
var month;
var programMonth = new Date().getMonth(); // runs Date().getMonth(); and puts it in programMonth
var day = new Date().getDate(); // Ditto.
var year = new Date().getFullYear(); // Ditto.

switch(programMonth) { // Programmatically takes variable of programMonth and figures out month via its variable
      case 0:
            month = "January";
            break;
      case 1:
            month = "February";
            break;
      case 2:
            month = "March";
            break;
      case 3:
            month = "April";
            break;
      case 4:
            month = "May";
            break;
      case 5:
            month = "June";
            break;
      case 6:
            month = "July";
            break;
      case 7:
            month = "August";
            break;
      case 8:
            month = "September";
            break;
      case 9:
            month = "October";
            break;
      case 10:
            month = "November";
            break;
      case 11:
            month = "December";
            break;
      default:
            month = "(Trouble getting month)";
            break;     
}

detectTimeParagraph = document.getElementById('time'); // Locate the <p> tag with the time class

detectTimeParagraph.innerHTML = `Today is ${month} ${day}, ${year}`; // Tell full month

Here’s the part of the head tag which contains both references, old and new:

<link rel="script" href="time.js"/>
<script type="text/javascript" src="time.js"></script>

Finally, here’s the actual paragraph element with its ID:

<p id="time"></p>

Thank you, Stack Overflow gurus!

EDIT: Fixed, somebody has linked me to a duplicate. I wasn’t able to find that in the duplicate list!

0 Answers0