• We just launched and are currently in beta. Join us as we build and grow the community.

Javascript - Simple Digital Clock

SloopNasty

Page Speed Optimizer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
42
Likes
48
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple Digital Clock using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used to your system that needed a timestamp. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display a digital clock. To create this just write these block of code inside the text editor and save this as index.html
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  5. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    Javascript - Simple Digital Clock</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dottec #ccc;"

    >
  17. <center

    >
  18. <div

    id

    =

    "myClockDisplay"

    style

    =

    "width:80%; font-size:60px; border:10px solid #000;"

    >
  19. </

    div

    >
  20. </

    center

    >
  21. </

    div

    >

  22. </

    body

    >
  23. <script

    src

    =

    "js/clock.js"

    ></

    script

    >
  24. <script

    >
  25. DisplayTime();
  26. </

    script

    >
  27. </

    html

    >

Creating the Script

This code contains the script of the application. This code will render a digital clock that runs base on you computer clock. To do this just copy write the code as shown below inside the text editor and save it as clock.js
  1. function

    DisplayTime(

    )

    {
  2. var

    date =

    new

    Date

    (

    )

    ;
  3. var

    hour =

    date.getHours

    (

    )

    ;
  4. var

    minute =

    date.getMinutes

    (

    )

    ;
  5. var

    seconds =

    date.getSeconds

    (

    )

    ;
  6. var

    day =

    "AM"

  7. if

    (

    hour ==

    0

    )

    {
  8. hour =

    12

    ;
  9. }

  10. if

    (

    hour >

    12

    )

    {
  11. hour-=

    12

    ;
  12. day =

    "PM"
  13. }

  14. hour =

    (

    hour <

    10

    )

    ?

    "0"

    +

    hour :

    hour;
  15. minute =

    (

    minute <

    10

    )

    ?

    "0"

    +

    minute :

    minute;
  16. seconds =

    (

    seconds <

    10

    )

    ?

    "0"

    +

    seconds :

    seconds;

  17. var

    currentTime =

    hour +

    ":"

    +

    minute +

    ":"

    +

    seconds +

    " "

    +

    day;
  18. document.getElementById

    (

    "myClockDisplay"

    )

    .innerText

    =

    currentTime;

  19. setInterval(

    DisplayTime,

    1000

    )

    ;
  20. }

There you have it we successfully created a Simple Digital Clock using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top