PDA

View Full Version : JavaScript


Hostboard
12-27-2020, 01:22 PM
I am trying to modify:


<script type="text/javascript">d = new Date();y = d.getFullYear();document.write(y);</script>


To output: January 1, 2020

I'm pretty sure the variables I need are: today.getMonth () and today.getDate()

I just can't seem to get the syntax correct...

shka
12-27-2020, 03:59 PM
You don't need <script type="text/javascript">, <script> is ok.

<script>
var d = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };
document.write(d.toLocaleDateString('en-US', options));
</script>

But better this way


<p id="datedemo"></p>
<script>
var d = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };
document.getElementById("datedemo").innerHTML = d.toLocaleDateString('en-US', options);
</script>


Examples https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString