
- 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++
The C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C. To access date and time related functions and structures, you would need to include <ctime> header file in your C++ program.
There are four time-related types: clock_t, time_t, size_t, and tm. The types - clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer.
The structure type tm holds the date and time in the form of a C structure having the following elements -
struct tm { int tm_sec; // seconds of minutes from 0 to 61 int tm_min; // minutes of hour from 0 to 59 int tm_hour; // hours of day from 0 to 24 int tm_mday; // day of month from 1 to 31 int tm_mon; // month of year from 0 to 11 int tm_year; // year since 1900 int tm_wday; // days since sunday int tm_yday; // days since January 1st int tm_isdst; // hours of daylight savings time }
Suppose you want to retrieve the current system date and time, either as a local time or as a Coordinated Universal Time (UTC). Following is the example to achieve the same –
Example
#include <iostream> #include <ctime> using namespace std; int main() { // current date/time based on current system time_t now = time(0); char* dt = ctime(&now); // convert now to string form cout << "The local date and time is: " << dt << endl; // convert now to tm struct for UTC tm *gmtm = gmtime(&now); dt = asctime(gmtm); cout << "The UTC date and time is:"<< dt << endl; }
Output
The local date and time is: Fri Mar 22 13:07:39 2019 The UTC date and time is:Fri Mar 22 07:37:39 2019
- Related Articles
- Print system time in C++ (3 different ways)
- Time-Sharing Operating system
- Difference Between Time Sharing and Real-Time Operating System
- How to print local time in Android sqlite?
- How to set default date time as system date time in MySQL?
- Get system start time from RuntimeMXBean in Java
- Operating system time slicing in round robin scheduling
- Print the time an hour ago PHP?
- What is Multitasking/Time Sharing Operating System?
- What is the Real Time Operating System?
- Speed Time Curve of Electrical Traction System
- What are the different tasks in the real time system?
- How to print current date and time in a JSP page?
- How to print date and time in specific format using JSP?
- How to print current date and time using Python?

Advertisements