Current Date and Time program in C/C++

Current Date and Time program in C/C++

The function struct tm *localtime(const time_t *timer) uses the timer with the values that represent the corresponding local time. The value is stored in structure tm and the information can be retrieved in different ways.
Syntax-
struct tm *localtime(const time_t (time(NULL))

The following return int values representing :
tm_sec => seconds, range 0 to 59
tm_min => minutes, range 0 to 59
tm_hour => hours, range 0 to 23
tm_mday => day of the month, range 1 to 31
tm_mon => month, range 0 to 11
tm_year => The number of years since 1900
tm_wday => day of the week, range 0 to 6
tm_yday => day in the year, range 0 to 365
tm_isdst => daylight saving time

Q.Write a program in C/C++ to print current/dynamic date and time.

1. Output-

2. Program in C :

#include<stdio.h>
#include<time.h>
#include<unistd.h>
void show(void);
int main() {
  show();
  return 0;
}
void show() {
  time_t sec = time(NULL);
  struct tm * t = localtime( & sec);
  printf("\n Today's date & time: ");
  printf("\n %02d/%02d/%04d", t -> tm_mday, t -> tm_mon + 1, 1900 + t -> tm_year);
  printf("\t%02d:%02d:%02d\n", t -> tm_hour, t -> tm_min, t -> tm_sec);
  sleep(0.59);
  system("cls");
  show();
}

3. Program in C++ :


#include<iostream>
#include<ctime>
#include<unistd.h>
#include<iomanip>
using namespace std;
void show(void);
int main() {
  show();
  return 0;
}
void show() {
  time_t sec;
  struct tm * t;
  sec = time(NULL);
  t = localtime( & sec);
  cout << "\n Today's date & time: ";
  cout << "\n " << setfill('0') << setw(2) << t -> tm_mday << "/" << setw(2) << t -> tm_mon + 1 << "/" << 1900 + t -> tm_year;
  cout << "\t" << setfill('0') << setw(2)<< t -> tm_hour << ":" << setw(2)<< t -> tm_min << ":" << setw(2) << t -> tm_sec << "\n";
  sleep(0.59);
  system("cls");
  show();
}

4. Explanation-

To display date and time we need the libraries <time.h> in C and <ctime> in C++. Using struct tm *localtime(const time_t (time(NULL)) mentioned above we can get hour, minutes, seconds, day, date, month, year etc. Combining these we can display date and time accordingly. We used “%02d” and setfill(‘0’) with setw(2) to print hours, seconds, date and month in 2-digit format. Then we used recursion to provide dynamic output. So first the date and time is printed and then we have used ‘sleep‘ function for 1 second and then the screen is cleared for new output. This continues till we close output screen. <unistd.h> library is needed in C/C++ for sleep(number_of_seconds); Finally we get current/dynamic date and time as output.

Note: “%02d” in C displays the integer in 2-digit format. setw(x) function in C++ is a function which displays any integer in ‘x’ space. But instead of blank spaces we can use setfill(character_to_be_filled). We need <iomanip> library for this in C++. So using “%02d” and setfill(‘0’) with setw(2), if integer is 5 (single digit) then 5 will be printed in a 2-digit format i.e ’05’.

5. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So this was the Current/Dynamic Date and Time program in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

Leave a Reply

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