
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print system time in C++ (3 different ways)
There are different ways by which system day, date and time can be printed in Human Readable Form.
First way
Using time() − It is used to find the current calendar time and have arithmetic data type that store time
localtime() − It is used to fill the structure with date and time
asctime() − It converts Local time into Human Readable Format
Day Month Date hour:month:second Year
Example
#include<iostream> #include<ctime> // used to work with date and time using namespace std; int main() { time_t t; // t passed as argument in function time() struct tm * tt; // decalring variable for localtime() time (&t); //passing argument to time() tt = localtime(&t); cout << "Current Day, Date and Time is = "<< asctime(tt); return 0; }
Output
if we run the above program then it will generate the following output
Current Day, Date and Time is = Tue Jul 23 19:05:50 2019
Second way
Chrono library is used to measure elapsed time in seconds, milliseconds, microseconds and nanoseconds
Example
#include <chrono> #include <ctime> #include <iostream> Using namespace std; int main() { auto givemetime = chrono::system_clock::to_time_t(chrono::system_clock::now()); cout << ctime(&givemetime) << endl; }
Output
if we run the above program then it will generate the following output
Current Day, Date and Time is = Tue Jul 23 19:05:50 2019
Third way
Example
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { time_t givemetime = time(NULL); printf("%s", ctime(&givemetime)); //ctime() returns given time return 0; }
Output
if we run the above program then it will generate the following output
Tue Jul 23 20:14:42 2019
- Related Articles
- Print system time in C++
- Different ways to print exception messages in Java
- What are the different ways to print an exception message in java?
- What are the different tasks in the real time system?
- Different ways to interact with SAP system from a web application
- Ways to print escape characters in C#
- Ways to print escape characters in python
- Different ways to concatenate Strings in Java
- Different ways to create Objects in Java
- Different Ways to Add Parentheses in C++
- 5 different ways to create objects in Java
- Different ways to overload a method in Java
- Different ways of Reading a file in C#
- Different ways to start a Task in C#
- Three Different ways to calculate factorial in C#

Advertisements