Submit your email address and receive
article and product notifications. Your email is safe with us.
Date object to display current date and time in alert window
We can display current date and time using JavaScript alert window by linking it to a button click event. We can place this button any where inside a page and display time and date once it is clicked by user. This is a simple script and we will try to develop better applications by learning such small examples.
We will be using built in Date object. We can create instances of this object and use in our script like this
var my_time = new Date()
Now this instance we can display by using JavaScript alert window.
alert(my_time);
Now this alert we will trigger when a button is clicked by a user. Here is onclick event of a button click.
Here is the complete code for the page with Show Time button.
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function show_now() {
var my_time = new Date();
alert(my_time);
}
</script>
</head>