SQL PHP HTML ASP JavaScript articles and free scripts to download
JavaScript Tutorials
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
JavaScript Forum
Subscribe
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.

<input type=button value="Show Time" onclick="show_now();">


Here is the demo, click this button



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>


<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<input type=button value="Show Time" onclick="show_now();">

</body>
</html>
Further readings
Countdown script displaying days , hours , minutes and seconds left from an event
Displaying changing Clock showing date and time in a web page
Timer function to set time for reminder alert
Timer Resetting the timer function
Displaying current date and time alert
Displaying Current Month using getMonth() function
Displaying present Year using getFullYear()
Showing Current Date using getDate() function
Displaying Current Day using getDay() function
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript