-2

I have a website which displays the month and day of the user visiting. It works fine right now, but when I update my code, the .getdate script stops working.

I am not altering any part of the script-- even when I copy and paste the same code that is working right now and upload it as a new file, the script does not work. I am a beginner and self-taught, so perhaps this has a very simple solution.

Here is the script:

<script>
function myFunction() {
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()] +' '+ [d.getDate()];
document.getElementById("date").innerHTML = n;
}
</script>

And here is how it is implemented in the code:

< h3 > today is < h3 id="date" >< /h3 >< h3 > and you can change the world< /h3 >< /h3 >

Again, as it is, the code is working on my website right now. However, any attempt to re-use the same code fails. It would stand to reason that I could copy and paste this code and it would achieve the same results. Any help is appreciated :)

Matthew
  • 13
  • 1
  • 1
    Are there any errors in the console or network tab when it does not work? – Taplar Jan 04 '19 at 23:08
  • 1
    I don't recommend using new Array() as it's slower and unnecessary :) – Kody R. Jan 04 '19 at 23:08
  • Your code doesn't actually do anything, it just defines a function. – melpomene Jan 04 '19 at 23:08
  • 1
    Are you trying to nest `h3` tags?! – melpomene Jan 04 '19 at 23:10
  • 1
    Are you actually calling the function anywhere with `myFunction()`? – Mark Jan 04 '19 at 23:12
  • Also `[d.getDate()]` works, but is almost certainly not what you intended. `[x]` creates a new array containing `x`, and the string concatenation will simply collapse the array again using `join`. I'd recommend taking a look at [this](https://stackoverflow.com/a/3552493/1715579) for some pointers. – p.s.w.g Jan 04 '19 at 23:12

1 Answers1

0

The problem seems to be the spaces that have snuck into the markup. Here is your snippet working. All I did was delete the spaces inside the tags.

function myFunction() {
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()] +' '+ [d.getDate()];
document.getElementById("date").innerHTML = n;
}
myFunction()
<h3> today is <h3 id="date" ></h3><h3> and you can change the world</h3></h3>

But while we're here, you should really avoid this kind of low level operation on dates, and use the built in functions. You can do what you want with...

var formattedDate = new Date().toLocaleString({},{month:'long', day:'numeric'})

...and it's automatically translated into the language of your user.

bbsimonbb
  • 20,571
  • 9
  • 59
  • 92