Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add current date using inline javascript

The date is not showing up in the output when I run the following code. I can't seem to figure out why the date won't show up...

<!DOCTYPE html>
<html>
<head>
 <title>document.write() Example</title>
</head>
<body>
 <p>The current date and time is:
 <script type=”text/javascript”>
 document.write(“<strong>” + (new Date()).toString() + “</strong>”);
 </script>
 </p>
</body>
</html>
like image 524
Goodword Avatar asked Jul 16 '26 07:07

Goodword


2 Answers

Because you are using these types of quotes: “ ... ”. You need to use " ... "

<script type="text/javascript">
    document.write("<strong>" + (new Date()).toString() + "</strong>");
</script>

Snippet:

 document.write("<strong>" + (new Date()).toString() + "</strong>");
like image 130
Spencer Wieczorek Avatar answered Jul 17 '26 20:07

Spencer Wieczorek


Do it as the way the samurais do it...

<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>

    <p>The current date and time is: <span id="myDate"></span> </p>

    <script>

      var d = new Date;
      var date = d.toString();

      span = document.getElementById('myDate');
      txt = document.createTextNode(date);
      span.innerText = txt.textContent;

    </script>


  </body>
</html>
like image 42
flaalf Avatar answered Jul 17 '26 19:07

flaalf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!