Spread the word

Tuesday, July 8, 2008

How to get the current time in JavaScript


JavaScript is very amazing scripting language through which you can do many funny things and can also satisfy useful tasks. Here is an very easy and useful use of JavaScript just to show you the use of a JavaScript's inbuilt function. You can easily show the current time in you website through JavaScript. JavaScript has an inbuilt function Date(), from which you can get the desired values. This is a good way to learn the Date() function. Checkout the example.

<html>
<head>
    <script language="JavaScript">
        function showTime(){
            time=new Date()
            document.write(time.getHours() + ":" + time.getMinutes())
        }
    </script>
</head>
<body>
    Welcome to my Site. The time now is  O’Clock.
</body>
</html>

This when seen in JavaScript enabled browser, will show up like this

Welcome to my Site. The time now is 10:30 O’Clock.

0 comments:

Post a Comment