Archive Pages Design$type=blogging

How to Create a Digital Clock in JavaScript?

In JavaScript, Date Object is used to work with date and time. You can create a digital clock in JavaScript for your web page manipulating ...


How to Create a Digital Clock in JavaScript?In JavaScript, Date Object is used to work with date and time. You can create a digital clock in JavaScript for your web page manipulating date object easily.  The Date object automatically hold the current date and time as its initial value. You can extract Hours, Minutes and Seconds from it, to create and display time on Digital Clock. I have presented here a simple and  easy to use JavaScript code to make a digital clock program. Feel free to use this code to add a digital clock on your web page or your web application.


Here I have defined a Date object with the new  keyword, new Date() to define the date object and extracted Hours, Minutes and Seconds using functions getHours(), getMinutes() and getSeconds() respectively and given codes for a simple clock having 24 hours format and a clock along with AM or PM.


JavaScript code for creating a simple digital clock


<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()


//to add a zero in front of numbers<10

m=checkTime(m)
s=checkTime(s)

document.getElementById('clock').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()', 500)
}

function checkTime(i)
{
if (i<10)
{ i="0" + i}
return i
}

window.onload=startTime;

</script>
<div id="clock" style="font-size:35px;color:#00aaff;text-shadow:2px 2px 2px blue;
border:3px groove blue;background-color:#aaffaa; width:195px; height:40px"></div>


checkTime() function is used here to add zero in front of numbers less then 10 and executed the function 'startTime()' using window.onload. Here is a inline css code to design the clock, you can change its values according to your wish. If you want to use the css in separate css file and link to your web page, you can write the css as follows on distinct css file.
#clock{

font-size:35px;
color:#00aaff;
text-shadow:2px 2px 2px blue;
border:3px groove blue;
background-color:#aaffaa;
width:195px;
height:40px;

}


JavaScript code for creating a digital clock with AM or PM


<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
var ap="AM";

//to add AM or PM after time

if(h>11) ap="PM";
if(h>12) h=h-12;
if(h==0) h=12;

//to add a zero in front of numbers<10

m=checkTime(m)
s=checkTime(s)

document.getElementById('clock').innerHTML=h+":"+m+":"+s+" "+ap
t=setTimeout('startTime()', 500)
}

function checkTime(i)
{
if (i<10)
{ i="0" + i}
return i
}

window.onload=startTime;

</script>

<div id="clock" style="font-size:35px;color:#00aaff;text-shadow:2px 2px 2px blue;
border:3px groove blue;background-color:#aaffaa; width:195px; height:40px"></div>


In the above code, variable ap is added to make the clock 12 hour format and to add "AM" or "PM" after the time and added this variable at innerHTML of clock element.


Preview of digital clock created with JavaScript







Related Posts


تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max6: How to Create a Digital Clock in JavaScript?
How to Create a Digital Clock in JavaScript?
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8hMHqiyi-2vy6sQ6A6pK7ylHVQsN0Y43LHPpMwfk-rjEKBr5ZblEyhJniE7GU-BwZvCq1dVz0NzJvys0e8S4koTqUx-JJLq7eZoAKbJL64xp11V0mivm1o5HZK2A13qNgZA4ynSJREG8g/s1600/clock.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8hMHqiyi-2vy6sQ6A6pK7ylHVQsN0Y43LHPpMwfk-rjEKBr5ZblEyhJniE7GU-BwZvCq1dVz0NzJvys0e8S4koTqUx-JJLq7eZoAKbJL64xp11V0mivm1o5HZK2A13qNgZA4ynSJREG8g/s72-c/clock.PNG
Max6
https://maxs29.blogspot.com/2014/02/how-to-create-digital-clock-in.html
https://maxs29.blogspot.com/
http://maxs29.blogspot.com/
http://maxs29.blogspot.com/2014/02/how-to-create-digital-clock-in.html
true
4303790511137642940
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت