Saturday, July 5, 2014

Display current time on webpage using Javascript

In this post i want to explain how to Display current time on webpage using Javascript.In this example i have used date and toLocaleTimeString methods.

toLocaleTimeString:
Return the time portion of a Date object as a string, using locale conventions.

Date():
The Date object lets us work with dates.

A date consists of a year, a month, a week, a day, a minute, a second, and a millisecond.

Date objects are created with the new Date() constructor.

There are 4 ways of initiating a date:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

setTimeout:
Display an alert box after xx seconds


Live DemoDownload Script

Before that we need to include jQuery Library file.Copy and Paste the following URL.
Library File:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>

jQuery Function

<script type="text/javascript">
            function ShowTime() {
                var dt = new Date();
                document.getElementById("time").innerHTML = dt.toLocaleTimeString();
                window.setTimeout("ShowTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
            }
        </script>

HTML File

<label id="time" style=" font-weight:bold"></label>

Please share your comments and feedback.Thanks.Please subscribe my updates via email.

No comments:

Post a Comment