How to output current date using javascript in twig

Viewed 76

I have a Twig template and inside of it I am trying to display the current date using JavaScript, but the date does not get rendered on the screen for some reason, why and how can I output the current date?

Here is my twig snippet:

 <p>By <a class="author-a">Kelly Chang</a> | <script>var today = new Date();
                        var dd = String(today. getDate()). padStart(2, '0');
                        var mm = String(today. getMonth() + 1). padStart(2, '0'); //January is 0!
                        var yyyy = today. getFullYear();
                        ​
                        today = mm + '/' + dd + '/' + yyyy
                        document.write(today);
                        </script>
                        </p>
1 Answers

Why overcomplicate this with javascript if you could do this in twig it self

<p>
    By <a class="author-a">Kelly Chang</a> | {{ "NOW" | date('d/m/Y') }}
</p>
Related