skip to Main Content

I have this javascript file:

var moment = require("moment")

var structuredDate = moment(Date()).format("LLLL")

And i wanted to change a <div> in html with the date.
I tried this:

<script type="text/javascript" src="bscript.js" async defer></script>
Welcome to my website<br>
Today is: <div id="date"></div>
<script>
    const date = document.getElementById("date")
    date.innerHTML=StructuredDate
</script>

But it is not working.
I use Browserify to bundle npm packages.

3

Answers


  1. It should structuredDate not StructuredDate.

    date.innerHTML = structuredDate;

    Login or Signup to reply.
  2. Main thing is that you have used the capital ‘S’ for the variable inside the tag js content. change that to
    date.innerHTML = structureDate

    And after that, you will see that undefined in the place where you need to see your date. Just copy and paste the following to set the date in that js file.

    let today = new Date().toLocaleDateString()

    var structuredDate = today

    This will provide you with the following output. I think that is your required output too.

    Output of the code

    Login or Signup to reply.
  3. I think your code is working with some changes like this:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
    <script type="text/javascript" src="bscript.js" async defer></script>
    Welcome to my website<br>
    Today is: <div id="date"></div>
    <script>
        var structuredDate = moment(Date.now()).format("LLLL");
        const date = document.getElementById("date")
        date.innerHTML=structuredDate
    </script>

    NOTE: This method is just applied only to client-side

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search