Search code examples
node.jsexpresshandlebars.jsexpress-handlebars

Todays Date in Handlebars


Can Someone Help me in getting Today's date in the handlebar file ? The Output should be something like "Today is Thursday" . `

<script>
  var today = new Date();
  var day = today.getDay();
  var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
  var x = document.getElementById("MyP").innerHTML;
  x = x + daylist[day];
  document.getElementById("MyP").innerHTML = x;
</script>

 <h2 class="page-header">Dashboard</h2>
<p>Welcome to {{user.username}}</p>
<p id="MyP"></p>

`

I've been trying this way but somehow it isn't working


Solution

  • Try this. Make sure the <script> tag is after your HTML since otherwise when your script executes, the HTML is not ready yet.

    <h2 class="page-header">Dashboard</h2>
    <p>Welcome to {{ user.username }}</p>
    <p id="MyP">Today is </p>
    
    <script>
        var today = new Date();
        var day = today.getDay();
        var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
        document.getElementById("MyP").innerHTML += daylist[day];
    </script>