Print system time in C++ (3 different ways)
First Method
Printing current date and time using time()
Second Method
// CPP program to print current date and time // using time and ctime. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { // declaring argument of time() time_t my_time = time(NULL); // ctime() used to give the present time printf("%s", ctime(&my;_time)); return 0; } |
Output:
It will show the current day, date and localtime, in the format Day Month Date hh:mm:ss Year
Third Method
Here we have used chrono library to print current date and time . The chrono library is a flexible collection of types that tracks time with varying degrees of precision .
The chrono library defines three main types as well as utility functions and common typedefs.
-> clocks
-> time points
-> durations
Code to print current date, day and time .
// CPP program to print current date and time // using chronos. #include <chrono> #include <ctime> #include <iostream> using namespace std; int main() { // Here system_clock is wall clock time from // the system-wide realtime clock auto timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); cout << ctime(&timenow;) << endl; } |
Output:
It will show the current day, date and localtime, in the format Day Month Date hh:mm:ss Year
Recommended Posts:
- C++ Program to print current Day, Date and Time
- C program to print digital clock with current time
- Time difference between expected time and given time
- Changing One Clock Time to Other Time in Minimum Number of Operations
- Program to find the time after K minutes from given time
- Ways to copy a vector in C++
- Different ways to initialize a variable in C/C++
- Initialize a vector in C++ (5 different ways)
- Preventing Object Copy in C++ (3 Different Ways)
- Array of Strings in C++ (3 Different Ways to Create)
- C++ 17 | New ways to Assign values to Variables
- Different ways to declare variable as constant in C and C++
- Different ways to delete elements in std::map (erase() and clear())
- system() in C/C++
- Add given n time durations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sanskar27jain



