Riley Lambert

Riley Lambert

1585122060

How to Build a Digital Clock using JavaScript

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.

Digital Clock in 24-Hour format

See the Pen Digital Clock 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

<div id="clock"></div>

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 _Orbitron_because it gives the look of a real digital clock. The body is given a dark background color and the text is center aligned.

CSS

/* Google font */
@import url('https://fonts.googleapis.com/css?family=Orbitron');

body {
  background-color: #121212;
}

#clock {
  font-family: 'Orbitron', sans-serif;
  color: #66ff99;
  font-size: 56px;
  text-align: center;
  padding-top: 40px;
  padding-bottom: 40px;
}

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

function currentTime() {
  var date = new Date(); /* creating object of Date class */
  var hour = date.getHours();
  var min = date.getMinutes();
  var sec = date.getSeconds();
}

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

function currentTime() {
  hour = updateTime(hour);
  min = updateTime(min);
  sec = updateTime(sec);
}

function updateTime(k) {
  if (k < 10) {
    return "0" + k;
  }
  else {
    return k;
  }
}

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

document.getElementById("clock").innerHTML = hour + " : " + min + " : " + sec; /* adding time to the div */

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

function currentTime() {
  var t = setTimeout(function(){ currentTime() }, 1000); /* setting timer */
}

currentTime(); /* calling currentTime() function to initiate the process */

So congratulations, you have just created a digital clock.

# Digital Clock in 12-Hour format

See the Pen Digital Clock 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

<div id="clock"></div>

The Styling

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

CSS

/* Google font */
@import url('https://fonts.googleapis.com/css?family=Orbitron');

body {
  background-color: #121212;
}

#clock {
  font-family: 'Orbitron', sans-serif;
  color: #66ff99;
  font-size: 56px;
  text-align: center;
  padding-top: 40px;
  padding-bottom: 40px;
}

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

function currentTime() {
  var date = new Date(); /* creating object of Date class */
  var hour = date.getHours();
  var min = date.getMinutes();
  var sec = date.getSeconds();
  hour = updateTime(hour);
  min = updateTime(min);
  sec = updateTime(sec);
  document.getElementById("clock").innerText = hour + " : " + min + " : " + sec; /* adding time to the div */
  var t = setTimeout(function(){ currentTime() }, 1000); /* setting timer */
}

function updateTime(k) {
  if (k < 10) {
    return "0" + k;
  }
  else {
    return k;
  }
}

currentTime(); /* calling currentTime() function to initiate the process */

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 _midday_if the number of hours is greater than or equal to 12 and “AM” otherwise.

JS

function currentTime() {
  var midday = "AM";
  midday = (hour >= 12) ? "PM" : "AM";
}

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

function currentTime() {
  hour = (hour == 0) ? 12 : ((hour > 12) ? (hour - 12): hour);
}

#javascript #webdev

What is GEEK

Buddha Community

How to Build a Digital Clock using JavaScript
anita maity

anita maity

1624368213

How to Create a digital clock with date using JavaScript

In this video you are going to learn how to create digital clock using HTML CSS and JavaScript programming code. Earlier I showed many more types of clock making designs. There are basically two types of watches, one analog and the other digital.

Live Demo

Video Tutorial

#html #css #javascript #digital #clock #javascript-clock

CSS Boss

CSS Boss

1606912089

How to create a calculator using javascript - Pure JS tutorials |Web Tutorials

In this video I will tell you How to create a calculator using javascript very easily.

#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html

DCODE SHOW

DCODE SHOW

1597918056

How To Make Digital Clock in JavaScript | Digital Clock in JS

In this tutorial, we will develop a digital clock using HTML, CSS, and JavaScript. It explains all the steps required to make a digital clock that updates itself every second to reflect the change in time.

https://youtu.be/M3blkxeQsVA

#digitalclock #setinterval javascript example #javascript clock #digital clock in js

anita maity

anita maity

1623522994

Build a Simple Digital Clock with JavaScript, HTML and CSS

In this article, I have shown you how to create a digital clock using HTML, CSS, and JavaScript programming code. If you are a beginner and want to know how to make a digital clock using JavaScript programming code, you can definitely find out from this article.

Demo & Source Code

#html #css #javascript #clock #digital

Why Use WordPress? What Can You Do With WordPress?

Can you use WordPress for anything other than blogging? To your surprise, yes. WordPress is more than just a blogging tool, and it has helped thousands of websites and web applications to thrive. The use of WordPress powers around 40% of online projects, and today in our blog, we would visit some amazing uses of WordPress other than blogging.
What Is The Use Of WordPress?

WordPress is the most popular website platform in the world. It is the first choice of businesses that want to set a feature-rich and dynamic Content Management System. So, if you ask what WordPress is used for, the answer is – everything. It is a super-flexible, feature-rich and secure platform that offers everything to build unique websites and applications. Let’s start knowing them:

1. Multiple Websites Under A Single Installation
WordPress Multisite allows you to develop multiple sites from a single WordPress installation. You can download WordPress and start building websites you want to launch under a single server. Literally speaking, you can handle hundreds of sites from one single dashboard, which now needs applause.
It is a highly efficient platform that allows you to easily run several websites under the same login credentials. One of the best things about WordPress is the themes it has to offer. You can simply download them and plugin for various sites and save space on sites without losing their speed.

2. WordPress Social Network
WordPress can be used for high-end projects such as Social Media Network. If you don’t have the money and patience to hire a coder and invest months in building a feature-rich social media site, go for WordPress. It is one of the most amazing uses of WordPress. Its stunning CMS is unbeatable. And you can build sites as good as Facebook or Reddit etc. It can just make the process a lot easier.
To set up a social media network, you would have to download a WordPress Plugin called BuddyPress. It would allow you to connect a community page with ease and would provide all the necessary features of a community or social media. It has direct messaging, activity stream, user groups, extended profiles, and so much more. You just have to download and configure it.
If BuddyPress doesn’t meet all your needs, don’t give up on your dreams. You can try out WP Symposium or PeepSo. There are also several themes you can use to build a social network.

3. Create A Forum For Your Brand’s Community
Communities are very important for your business. They help you stay in constant connection with your users and consumers. And allow you to turn them into a loyal customer base. Meanwhile, there are many good technologies that can be used for building a community page – the good old WordPress is still the best.
It is the best community development technology. If you want to build your online community, you need to consider all the amazing features you get with WordPress. Plugins such as BB Press is an open-source, template-driven PHP/ MySQL forum software. It is very simple and doesn’t hamper the experience of the website.
Other tools such as wpFoRo and Asgaros Forum are equally good for creating a community blog. They are lightweight tools that are easy to manage and integrate with your WordPress site easily. However, there is only one tiny problem; you need to have some technical knowledge to build a WordPress Community blog page.

4. Shortcodes
Since we gave you a problem in the previous section, we would also give you a perfect solution for it. You might not know to code, but you have shortcodes. Shortcodes help you execute functions without having to code. It is an easy way to build an amazing website, add new features, customize plugins easily. They are short lines of code, and rather than memorizing multiple lines; you can have zero technical knowledge and start building a feature-rich website or application.
There are also plugins like Shortcoder, Shortcodes Ultimate, and the Basics available on WordPress that can be used, and you would not even have to remember the shortcodes.

5. Build Online Stores
If you still think about why to use WordPress, use it to build an online store. You can start selling your goods online and start selling. It is an affordable technology that helps you build a feature-rich eCommerce store with WordPress.
WooCommerce is an extension of WordPress and is one of the most used eCommerce solutions. WooCommerce holds a 28% share of the global market and is one of the best ways to set up an online store. It allows you to build user-friendly and professional online stores and has thousands of free and paid extensions. Moreover as an open-source platform, and you don’t have to pay for the license.
Apart from WooCommerce, there are Easy Digital Downloads, iThemes Exchange, Shopify eCommerce plugin, and so much more available.

6. Security Features
WordPress takes security very seriously. It offers tons of external solutions that help you in safeguarding your WordPress site. While there is no way to ensure 100% security, it provides regular updates with security patches and provides several plugins to help with backups, two-factor authorization, and more.
By choosing hosting providers like WP Engine, you can improve the security of the website. It helps in threat detection, manage patching and updates, and internal security audits for the customers, and so much more.

Read More

#use of wordpress #use wordpress for business website #use wordpress for website #what is use of wordpress #why use wordpress #why use wordpress to build a website