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.
basics clock date digital examples getHours getMinutes getSeconds