Yogesh Chauhan's Blog

How to create a simple digital clock using JavaScript?

in JavaScript on 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.


amazon

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 add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
What is the best way to add JavaScript Code into HTML?HTMLHow to Recognize an Array in JavaScript?JavaScriptEffects in ReactReactHow to add Local State to a Class in React?React6 Different Functions to Sort Arrays in PHPPHPUse inline if to make a shorter conditional syntax in ReactReactHow to switch dark and light themes using pure CSS?CSSDebugging in WordPress Part 3: SCRIPT_DEBUG and SAVEQUERIESWordPressHow to change style of all direct children of an element using jQuery?jQueryWordPress: How to find all posts with a specific custom field value?WordPressSolution for “Yarn build: Failed because of a stylelint error”WordPressHow to create a for loop in SCSS (Sass)?SCSS