Home > Web Front-end > JS Tutorial > body text

How to create an analog clock using HTML, CSS and JavaScript?

PHPz
Release: 2023-08-29 11:01:02
forward
810 people have browsed it

如何使用 HTML、CSS 和 JavaScript 创建模拟时钟?

In this tutorial we will design an analog clock with the help of HTML and CSS and use JavaScript Its working, this will display the current time in hours, minutes and seconds format.

Approach
  • We will use the Date object and With some calculations, we will display hours, minutes, and seconds.

  • We will get the system time from a JavaScript object Date() which has functions such as >getHours(), getSecond() and getMinutes().

  • After getting we will apply the hours , minutes and seconds formats to convert all three-hand hours, minutes and seconds of rotation.

The analog clock displays time through three hands that move continuously and mark the time from 1 to 12 as it is displayed.

HTML

Create an HTML file named index.html and add some boilerplate code, this is the basic html syntax as shown below, inside our HTML code there is a div named "sizeOfAnalog", has the same class name "sizeOfAnalog" inside these three divs, with names hour_clock, minutes_clock, seconds_clock.

CSS

We will add inner CSS as well as inner JavaScript in the HTML code. Use the tag to apply internal CSS and to apply internal JavaScript.

JavaScript

In the JavaScript part, we do the main logic work. We will use the JavaScript Date() object and its functions getHours(), getSecond(), and getMinutes() to get the current time in hours, minutes, and seconds. Once we have the hours, minutes, and seconds format, we will apply the rotation that converts all three hands of the hours, minutes, and seconds.

Steps

Step 1 - We use the "background: url(clock.png) no-repeat;"code to apply the clock on the screen The background image, the no-repeat here is to prevent the image from repeating itself.

Step 2 - Now we will get the current time using Date() object and we can also get the current hour, minutes and seconds respectively from the Date object. Now we will get the hours, minutes and seconds from the HTML code and convert the rotation of the hand to 360 degrees.

Step 3 - As we know, degrees after 360 degrees will be considered as one rotation. So if you want to get a total of 12 hours from these 360 ​​degrees, you can do it using (360/12), which will give you 30 degrees in one hour.

Step 4 - Same per minute as we need a total of 360 minutes for 60 minutes so this can be done using the required (360/60) = 6 degrees per minute.

Step 5 - In the JavaScript part using setInterval() we can run the set clock every 1000ms (milliseconds) since 1 second equals 1000ms.

Therefore, we use new to create a date object Date()

Get the hours between 0 and 23getHours()

Get the minutes between 0 and 59getMinutes()

Get the seconds between 0 and 59getSeconds()

Step 6 - Once we have all three values, we will rotate each hand and our analog clock will start showing the time.

Example (Full Program)

Below is the complete code to create an analog clock using HTML, CSS and JavaScript.




   Analog Clock Tutorials Point
   

Copy after login

The above is the detailed content of How to create an analog clock using HTML, CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!