+6 votes
8.3k views
in Programming by (270 points)
i want a button and onClick  on that button current date and time in java script should populate .

in ui only button should show time and date should come after click.

Thanks for answer!
closed

1 Answer

+3 votes
by (1.9k points)
edited by
 
Best answer

Hello navneet!

you can use java script like this 

<p id="display"></p>
 
<button type="button" onclick="myFunction()">Click me to get time</button>
 
<script>
function myFunction()
{
document.getElementById("display").innerHTML = Date();
}
</script>
here <p> is the place where the date and time will be shown.
in <p> output will come.
 
 
0
by (270 points)
thanks its working.
0
by Expert (5.1k points)
<html>
<head>
<script>
function ShowLocalDate()
{
var dNow = new Date();
var localdate= (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
document.getElementById("currentDate").innerHTML=localdate;
}
 
</script>
</head>
<body>
 
<label id="currentDate"></p>
<button type="button" onclick="ShowLocalDate()">Show Local DateTime</button>
 
</body>
</html>

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated