Create a Digital Clock in C++

In this tutorial, we are going to learn how to create a digital clock in C++. We can use time() and localtime() function to get the local time and then print it on the console. See how it can be done.

This digital clock will update the time automatically with an interval of 1 second in run time.

Digital Clock in C++

These are the steps to create a digital clock in C++.

  • call time() function with argument ‘0’ to store total seconds in total_seconds variable.
  • make a call to localtime() function and pass the address of total_seconds as a parameter. This function splits the seconds into hours, minutes and seconds and returns a structure tm* which holds the values of hours, minutes and seconds. To know more about this, read this: Date and Time in C++
  • Get the values of seconds, minutes and hours from the structure variable.
  • Print the values of hours, minutes and seconds as shown in the example program with an interval of 1 sec. Use the system(“CLS”) statement to clear the screen for each interval.
  • The localtime() function returns time in 24-hour format. Convert it into the 12-hour format and print AM or PM accordingly.

Here, you can see the example program.

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
  int sec_prev=0;
  while(1)
  {
    int seconds, minutes, hours;
    string str;
  
    //storing total seconds
    time_t total_seconds=time(0);
  
    //getting values of seconds, minutes and hours
    struct tm* ct=localtime(&total_seconds);
  
    seconds=ct->tm_sec;
    minutes=ct->tm_min;
    hours=ct->tm_hour;
    
    //converting it into 12 hour format
    if(hours>=12)
      str="PM";
    else
      str="AM";
    hours=hours>12?hours-12:hours;	
    
    
    //printing the result
    if(seconds==sec_prev+1 || (sec_prev==59 && seconds==0))
    {
      system("CLS");
      cout<< (hours<10?"0":"") << hours <<":" << (minutes<10?"0":"") << minutes << ":" << (seconds<10?"0":"") << seconds << " " << str <<endl;
    }
    
    sec_prev=seconds;
    
  }
  
  return 0;
}

 

You may also learn: C++ program to create a stopwatch

The output of the program will be like this and is updated every second:

06:56:07 AM
06:56:08 AM
06:56:09 AM

Explanation

As explained above, we first call time() function by passing 0 as a parameter which gives us total seconds. The localtime() function converts these seconds into hours, minutes and seconds in 24-hour format. We print the result after converting it to the 12-hour format. If the hour value is greater than 12 we subtract 12 from it otherwise we print it as it is. If the hour value is 12 or greater than 12, we add PM in the result otherwise we add AM. We have used a sec_prev variable to store the value of seconds of the time value obtained from the previous execution of the while loop so that we can compare it to the current value of seconds and when the difference between them is 1 sec, we can clear the screen using system(“CLS”) and print the result in required format.

Also read: Ternary Operator in C++

Leave a Reply

Your email address will not be published. Required fields are marked *