Simple Digital Clock

broken image


In this post, you will learn how to create a Digital Clock in 24-hour and 12-hour formats using JavaScript.

In the 24-hour format, time is displayed in the form of HH : MM : SS. In the 12-hour format, it is displayed in the form of HH : MM : SS AM/PM.

The sound could be as simple. Some special clocks are often used by people who are blind or cannot see well enough to read an analog or digital clock. Our mission is simple to deliver great products and make it easy. And get back to using your brainpower for the important things in life like pondering the question, 'What should I binge watch this weekend?' Digital Alarm Clock with Radio: Wake up to an alarm or to your favorite radio station; Snooze button; Digital AM/FM.

# Digital Clock in 24-Hour format

See the Pen Digital Clock by Aakhya Singh (@aakhya) on CodePen.

Let's start with the HTML.

The Structure

To begin with, create a div with id clock in which you want to display time. We will insert the time into this div using JavaScript.

HTML

The Styling

The styling for the text to be displayed in the div is defined in the CSS.

The text is given a font size and color. Its font family is chosen as Orbitronbecause it gives the look of a real digital clock. The body is given a dark background color and the text is center aligned.

CSS

Simple

The Scrypting

Now here comes the main part. The entire code for the working of the clock is written within the currentTime() function.

Inside this function, an object of the Date Class is created which allows you to call year, date, hour, minute, second and millisecond.

In our code, this object is used for getting the current hours, minutes and seconds which are stored in different variables.

JS

Simple Digital Clock

The obtained hours, minutes and seconds will be displayed in single digit if less than 10. For example, the current hour will be displayed as 7 instead of 07. To always display the elements of time in two-digit format, a 0 is appended before them whenever they are less than 10 using the updateTime() method.

JS

Now once our time is ready, let's display it in the div which we made before. This is done by obtaining the div using the document.getElementById method and give our time as the content of the div using the innerHTML property.

JS

To start the timer, setTimeout() method is used which takes the function currentTime() as the first argument and the time (in milliseconds) after which you wish to call the function as the second argument. In our case, the function is called after every 1000 milliseconds.

Finally, the currentTime() function is called to initiate the whole process.

JS

So congratulations, you have just created a digital clock.

# Digital Clock in 12-Hour format

See the Pen Digital Clock by Aakhya Singh (@aakhya) on CodePen.

The HTML and CSS for 12-hour format will remain the same as in the previous case.

The Structure

Create a div to display time as done for the previous clock.

HTML

The Styling

Style the clock in the same way as done for the previous clock.

CSS

The Scrypting

The JavaScript code will have some extra lines in addition to the code created for the previous clock. The code for the digital clock in 24-hour format is shown below.

JS

Now we have to add just three more lines of code and our clock will be ready.

A variable named midday is created and an initial value of 'AM' is assigned to it. This variable will be used to store 'AM' or 'PM'.

Further, ternary operator is used to give the value 'PM' to the variable middayif the number of hours is greater than or equal to 12 and 'AM' otherwise.

JS

In order to display time in 12-hour format, the number of hours should not be greater than 12. Therefore, 12 is deducted from the number of hours if it is greater than 12.

Also, the 24-hour format of 00 : 01 : 23 at midnight should be displayed as 12 : 01 : 23 AM in the 12-hour format. For this, the number of hours 00 is replaced by 12.

JS

121

Neat and simple digital clock ideal for study and kitchen.
1. Optimized battery usage.
2. Swipe up/down to adjust brightness.
3. Swipe left/right to change color.
4. Double tap to sleep and wake display [ power save mode ].
5. Options to show seconds and 24 hour format.

To Download Digital Clock For PC,users need to install an Android Emulator like Xeplayer.With Xeplayer,you can Download Digital Clock for PC version on your Windows 7,8,10 and Laptop.

How to Play Digital Clock on PC,Laptop,Windows

Simple Digital Clock

The Scrypting

Now here comes the main part. The entire code for the working of the clock is written within the currentTime() function.

Inside this function, an object of the Date Class is created which allows you to call year, date, hour, minute, second and millisecond.

In our code, this object is used for getting the current hours, minutes and seconds which are stored in different variables.

JS

The obtained hours, minutes and seconds will be displayed in single digit if less than 10. For example, the current hour will be displayed as 7 instead of 07. To always display the elements of time in two-digit format, a 0 is appended before them whenever they are less than 10 using the updateTime() method.

JS

Now once our time is ready, let's display it in the div which we made before. This is done by obtaining the div using the document.getElementById method and give our time as the content of the div using the innerHTML property.

JS

To start the timer, setTimeout() method is used which takes the function currentTime() as the first argument and the time (in milliseconds) after which you wish to call the function as the second argument. In our case, the function is called after every 1000 milliseconds.

Finally, the currentTime() function is called to initiate the whole process.

JS

So congratulations, you have just created a digital clock.

# Digital Clock in 12-Hour format

See the Pen Digital Clock by Aakhya Singh (@aakhya) on CodePen.

The HTML and CSS for 12-hour format will remain the same as in the previous case.

The Structure

Create a div to display time as done for the previous clock.

HTML

The Styling

Style the clock in the same way as done for the previous clock.

CSS

The Scrypting

The JavaScript code will have some extra lines in addition to the code created for the previous clock. The code for the digital clock in 24-hour format is shown below.

JS

Now we have to add just three more lines of code and our clock will be ready.

A variable named midday is created and an initial value of 'AM' is assigned to it. This variable will be used to store 'AM' or 'PM'.

Further, ternary operator is used to give the value 'PM' to the variable middayif the number of hours is greater than or equal to 12 and 'AM' otherwise.

JS

In order to display time in 12-hour format, the number of hours should not be greater than 12. Therefore, 12 is deducted from the number of hours if it is greater than 12.

Also, the 24-hour format of 00 : 01 : 23 at midnight should be displayed as 12 : 01 : 23 AM in the 12-hour format. For this, the number of hours 00 is replaced by 12.

JS

121

Neat and simple digital clock ideal for study and kitchen.
1. Optimized battery usage.
2. Swipe up/down to adjust brightness.
3. Swipe left/right to change color.
4. Double tap to sleep and wake display [ power save mode ].
5. Options to show seconds and 24 hour format.

To Download Digital Clock For PC,users need to install an Android Emulator like Xeplayer.With Xeplayer,you can Download Digital Clock for PC version on your Windows 7,8,10 and Laptop.

How to Play Digital Clock on PC,Laptop,Windows

Simple Digital Clock Circuit

1.Download and Install XePlayer Android Emulator.Click 'Download XePlayer' to download.

Simple Digital Clock Circuit Diagram

2.Run XePlayer Android Emulator and login Google Play Store.

3.Open Google Play Store and search Digital Clock and Download,

How To Make A Simple Digital Clock

Or Import the apk file from your PC Into XePlayer to install it.

Simple Digital Clock Circuit

4.Install Digital Clock for PC.Now you can play Digital Clock on PC.Have fun!





broken image