YogeshChauhan.com

How to create a simple digital clock using JavaScript?

March 29, 2021

HTML

CSS

JavaScript

This is most important part. We’ll make a function and then call it at the end. Creating a function will be helpful to reuse or export.

We need to get the time in current hour, minute and second. We’ve used Date() function in past in few examples such as to convert datetime to date format. Using the new date we can get the hour(getHours), minutes(getMinutes) and seconds(getSeconds).

I have also used The Conditional Operator Shorthand to check and make the minute and second digit a double digit.

After that just set those values in our HTML elements using their IDs.

We need to keep the clock updated. We can use setTimeout function to do so.

Here’s the function and the demo.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

How to create a sidebar using pure CSS?CSSHow to check if the element exists using JavaScript and jQuery?JavaScriptALTER DATABASE in PostgreSQLPostgresCSS Attribute SelectorsCSSSome SQL LIKE Operators We Need to Keep in MindSQL/MySQLSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscHow Do You Make a Private VPN?MiscHow to Remove PHP File Extensions From Your Website URLs?PHPWhat’s a Web Storage API in JavaScript?JavaScriptAll different methods for accessing elements in the DOM using JavaScriptJavaScriptCreate a responsive pricing table using simple HTML and CSSCSSHow to host Google fonts on your server and add them using CSS?CSS