Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » JavaScript » Digital clock using JavaScript

Digital clock using JavaScript

In this article you will learn how to create the digital clock in JavaScript.

Page Views : 31016
Downloads : 183
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Digital_Clock.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Simple example to create the digital clock using JavaScript:

 

<html>

  <head><title>Digital Clock</title>

    <style>

    .styling

    {

    background-color:"#ffffcc";

    color:navy;

    font: 20px MS Sans Serif;

    padding: 6px;

    }

    </style>

  </head>

  <body bgcolor="#ccccff">Digital Clock: <br /><br />

    <span id="digitalclock" class="styling"></span>

      <script>

      var alternate=0

      var standardbrowser=!document.all&&!document.getElementById

 

      if (standardbrowser)

      document.write('<form name="form1"><input type="text" name="text1" size="11"></form>')

 

      function show()

      {

        if (!standardbrowser)

        var clockobj=document.getElementById? document.getElementById("digitalclock") : document.all.digitalclock

        var Digital=new Date()

        var hours=Digital.getHours()

        var minutes=Digital.getMinutes()

        var dn="AM"

 

        if (hours==12) dn="PM"

        if (hours>12)

        {

          dn="PM"

          hours=hours-12

        }

        if (hours==0) hours=12

        if (hours.toString().length==1)

        hours="0"+hours

        if (minutes<=9)

        minutes="0"+minutes

 

        if (standardbrowser)

        {

          if (alternate==0)

          document.form1.text1.value=hours+" : "+minutes+" "+dn

          else

          document.form1.text1.value=hours+"   "+minutes+" "+dn

        }

        else

        {

          if (alternate==0)

          clockobj.innerHTML=hours+"<font color='#ff0033'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"

          else

          clockobj.innerHTML=hours+"<font color='blue'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"

        }

        alternate=(alternate==0)? 1 : 0

        setTimeout("show()",1000)

      }

      window.onload=show()

    </script>

  </body>

</html>

 

Output: Output of the above script is something as follows:  

 

 

 

Figure: Digital clock.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore
I am having a Master Degree in Computer Application and I am in this field from last one year and so. I really like to work in the .NET platform.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
I like this file by chandara On February 11, 2009
I want to you to help me. I want to design digital clock that it has second is running with minute and hour. It means it is displayed hour, minute and second are running. Thanks
Reply | Email | Modify 
Re: I like this file by Purushottam On February 15, 2009

Hello friend

Thanks to support me.

U can try this script to display time with Hours, minuts and seconds.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DigitalClock.aspx.cs" Inherits="DigitalClock" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>DigitalClock</title>

<script type="text/javascript">

/* showClock() function extracts the current time hours, minutes and seconds and then displays them in the div with showText

id from the BODY section */

function showClock()

{

var clock=new Date();

var hours=clock.getHours();

var minutes=clock.getMinutes();

var seconds=clock.getSeconds();

// for a nice disply we'll add a zero before the numbers between 0 and 9

if (hours<10)

{

hours="0" + hours;

}

if (minutes<10)

{

minutes="0" + minutes;

}

if (seconds<10)

{

seconds="0" + seconds;

}

document.getElementById('showText').innerHTML=hours+":"+minutes+":"+seconds;

t=setTimeout('showClock()',500);

/* setTimeout() JavaScript method is used to call showClock() every 1000 milliseconds (that means exactly 1 second) */

}

</script>

</head>

<body onload="showClock()">

<div id="showText">

</div>

<form id="form1" runat="server">

<div>

</div>

</form>

</body>

</html>

Reply | Email | Modify 
Free JavaScript code for Digital Clock by avaneet On March 23, 2009
Free JavaScript code for Digital Clock at: http://www.etechplanet.com/post/2009/03/22/Digital-Clock-using-JavaScript.aspx
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.