0

as i try the below javascript code to display date

<!DOCTYPE html>
<html>
<body>
<h1> javascript</h1>
<button type="button" onclick=document.getElementById('demo').innerHTML=Date()">click me to display date and time.</button>
<p id="demo"></p>
</body>
</html>

4 Answers 4

4

There were few corrections:

<!DOCTYPE html>
<html>

<body>
  <h1> javascript</h1>
  <!-- added missing qoutes for onclick -->
  <button type="button" onclick="document.getElementById('demo').innerHTML= new Date();">click me to display date and time.</button>
  <!-- added missing, new keyword for Date() -->
  <p id="demo"></p>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

You have missed the quotes in onclick="..."

<!DOCTYPE html>
<html>
<body>
<h1> javascript</h1>
<button type="button" onclick="document.getElementById('demo').innerHTML=Date()">click me to display date and time.</button>
<p id="demo"></p>
</body>
</html>

Comments

0

Here is the code will be helpful...

var date = new Date();
var n = date.toDateString();
var time = date.toLocaleTimeString();

alert(n + ' ' + time);

you can even put this in HTML DIV tag as per you need

JS

document.getElementById('time').innerHTML = n + ' ' + time;

HTML

<div id='time'></div>

Comments

0
onclick=document.getElementById('demo').innerHTML=Date()">click me to display

This needs to be fixed. When you wrote onclick= you don't have a " at the beginning. Simple Fix, add it:

onclick="document.getElementById('demo').innerHTML=Date()">click me to display

Working Fix: https://jsfiddle.net/4cjhg2uo/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.